I am creating asp.net web application with web forms, I am using register routes to product friendly URL’s.
Following is the code in Global.asax.cs;
void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
RegisterRoutes(RouteTable.Routes);
}
void RegisterRoutes(RouteCollection routes)
{
// Register a route for Categories/All
routes.MapPageRoute(
"All Categories", // Route name
"Categories/All", // Route URL
"~/AllCategories.aspx" // Web page to handle route
);
// Route to handle Categories/{CategoryName}.
// The {*CategoryName} instructs the route to match all content after the first slash, which is needed b/c some category names contain a slash, as in the category "Meat/Produce"
// See http://forums.asp.net/p/1417546/3131024.aspx for more information
routes.MapPageRoute(
"View Category", // Route name
"Categories/{*CategoryName}", // Route URL
"~/CategoryProducts.aspx" // Web page to handle route
);
// Register a route for Products/{ProductName}
routes.MapPageRoute(
"View All Product", // Route name
"Products", // Route URL
"~/ViewProducts.aspx" // Web page to handle route
);
// Register a route for Products/{ProductName}
routes.MapPageRoute(
"View Product", // Route name
"Product/{ProductName}", // Route URL
"~/ViewProduct.aspx" // Web page to handle route
);
// Register a route for Products/{ProductName}
routes.MapPageRoute(
"Add Product", // Route name
"NewProduct", // Route URL
"~/AddProduct.aspx" // Web page to handle route
);
}
Now in one page when I put
lnkNewProduct.NavigateUrl = Page.GetRouteUrl("Add Product");
It produce wrong href url when I run the project.
Can anyone please tell why this happen? At moment it is showing URL like http:\localhost:5770\Categories\All?Length=11… this is hard to understand.
Any hint or help???
Thanks
You need to use the correct overload:
This
GetRouteUrloverload uses the RouteName.You can use Route Url expressions also:
http://msdn.microsoft.com/en-us/library/system.web.compilation.routeurlexpressionbuilder.aspx