Monday, 28 January 2013 13:57

asp.net routing for url rewriting

Written by 
Rate this item
(0 votes)

asp.net routing is easy to use for rewriting any url.

if you have a url like wwww.aspdotnetsite.com/Projects?user=username.
but if you want to have it in short name for marketing or for anything
you want like this is the resulting url -
wwww.aspdotnetsite.com/Projects/username

then following is the way how to do it -

in your site in global.asax code behind file add the routing this way -

public static void RegisterRoutes(RouteCollection routeCollection)
{
routeCollection.RouteExistingFiles = false;
routeCollection.MapPageRoute("RouteForCustomer", "Projects/{Id}", "~/Projects.aspx");
}
protected void Application_Start(object sender, EventArgs e)
{
RegisterRoutes(RouteTable.Routes);
}

here you define how the route url will look like and the physical file
Projects.aspx in manageRoute method.

then in the Projects.aspx page load method you can access the username value
this way -

string User_Name = Page.RouteData.Values["Id"].ToString();

Read 3057 times
Super User

Email This email address is being protected from spambots. You need JavaScript enabled to view it.

Latest discussions

  • No posts to display.