Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET
Hi All,
 
How to write easy Routing URLs, how to cal route from global.asax
 

my global.asax code like is
 
protected void Application_Start(object sender, EventArgs e)
{
 
    // Add Routes.
    RegisterRoutes(RouteTable.Routes);
 
}
void RegisterRoutes(RouteCollection routes)
{
    routes.MapPageRoute(
        "HomeRoute",
        "Home",
        "~/Employer/Employer_Login.aspx"
    );
}
 
how to cal the Routes in Employer_login.aspx page
 
please help me.
Posted 7-Feb-13 5:13am

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

You can rewrite/route the URL before it actually hits a page, to do this add your routing code in global.asax's
 
protected void Application_BeginRequest(object sender, EventArgs e)
 
event. If you are already on the page (eg Employer_login.aspx) you can use
 
Response.Redirect("newurl")
 
To redirect a URL in Application_BeginRequest you can use:
 
Context.RewritePath("new URL comes here", true);
 
to send someone to a different URL.
 
and Request.Url to check the URL the user entered, get the QueryString, etc.
 
Here's a simple example that will add .aspx to any URL entered where there is an .aspx file at the location but the URL was specified without it:
(eg user entered yourwebsite/default and you wish to change it to yourwebsite/default.aspx):
 
//The physical file that has been requested.
var requestedfile = Server.MapPath(Request.Url.AbsolutePath);
//The extension of the file.
var filetype = Path.GetExtension(requestedfile).ToLower();
//The query parameters as the sole parameters of the url
var incomingparamsdir = incomingparams.Length > 0 ? "?" + incomingparams : "";
 
if ((filetype == "." || filetype == "") && requestedfile.Length > 0)
{
    filetype = ".aspx";
    requestedfile = Path.ChangeExtension(requestedfile, ".aspx");
    if (File.Exists(requestedfile))
    {
        Context.RewritePath(Path.ChangeExtension(Request.Url.AbsolutePath, ".aspx") + incomingparamsdir);
        return;
    }
}
 

Hope this solves your issue.
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 468
1 Sergey Alexandrovich Kryukov 305
2 PhilLenoir 115
3 RajeeshMenoth 110
4 Uma Shankar Patel 80
0 Sergey Alexandrovich Kryukov 6,557
1 OriginalGriff 5,966
2 CPallini 2,473
3 Richard MacCutchan 1,687
4 Abhinav S 1,540


Advertise | Privacy | Mobile
Web01 | 2.8.140821.2 | Last Updated 8 Feb 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100