If you are using ASP.NET 4.0 and IIS 7 or higher it is easy
to do URL routing in asp.net application. You have to do only 2 steps to setup URL routing
1) Create a function to register routes in
global.asax
void RegisterRoutes(RouteCollection routes)
{
routes.Ignore("{resource}.aspx/{*pathInfo}");
routes.MapPageRoute(
"", // Route name
"Home", // Route URL
"~/Default.aspx"
// Web page to handle route
);
}
2) Invoke the function from
Application_Start method
void Application_Start(object
sender, EventArgs e)
{
// Code
that runs on application startup
RegisterRoutes(RouteTable.Routes);
}
Now any request to http://webiste/Home
will be redirected to default.aspx page
Now how to pass parameters? You can add parameters to the
route URL as given below
routes.MapPageRoute(
"About", // Route name
"AboutTest/{controller}/{action}/{id}", // Route URL
"~/About.aspx",
// Web page to handle route
true,
new
RouteValueDictionary { { "controller", "food"
}, { "action", "show" },{"id","myid"} });
The
parameters can be accessed from the page using “Page.RouteData.Values”
if
(Page.RouteData.Values["controller"]
!= null)
{
Response.Write(Page.RouteData.Values["controller"]);
}
if
(Page.RouteData.Values["action"]
!= null)
{
Response.Write(Page.RouteData.Values["action"]);
}