Click here to Skip to main content
12,620,909 members (32,441 online)
Rate this:
 
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 6: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
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web01 | 2.8.161128.1 | Last Updated 8 Feb 2013
Copyright © CodeProject, 1999-2016
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