Click here to Skip to main content
15,897,090 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi All,

How to write easy Routing URLs, how to cal route from global.asax


my global.asax code like is

C#
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

1 solution

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):

C#
//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.
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900