Quick ASP.NET routing question
How can I turn this /Home/About/
into just /Abou开发者_运维百科t/
using the rules in the Global.aspx file?
public static void RegisterRoutes(RouteCollection routes)
{
routes.MapRoute(
"About", // Route name
"About", // URL
new { controller = "Home", action = "About" });
...
}
@Scott: Thanks. Fixed it.
routes.MapRoute(
"About",
"About",
new { controller = "Home", action = "About" }
);
Just make sure it is place before the default route handler.
Add a route:
routes.MapRoute(
"About",
"About"
new { controller = "Home", action = "About" });
Since it is hardcoded, you want to make sure it is before any routes that have placeholders for the various params.
Create a new route before the default route like this:
routes.MapRoute("about", "About", new { controller = "YourController", action = "About" });
this will give you the explanation too :) http://weblogs.asp.net/scottgu/archive/2007/02/26/tip-trick-url-rewriting-with-asp-net.aspx
I used this:
routes.MapRoute(
"HomeActions",
"{action}",
new
{
controller = "Home",
action = "Index" // I technically don't think this is required.
},
new // The second object are route constraints
{
action = "Index|FAQ|ContactUs|Examples|Materials|Members" // <-- various actions on my home controller. Any regex works here.
});
精彩评论