ASP.NET MVC 3.0 Routing
I have Controller Blog
I have Action Index()
and Show(stirng id)
Index()
displaying all posts and Show(sting id)
displaying single post
I want to map Blog/Show/开发者_StackOverflow社区id
to responce on Blog/id
So i went to Global.asxc and did that:
routes.MapRouteLowercase(
"Blog",
"Blog/{id}",
new { controller = "Blog", action = "Show", id = UrlParameter.Optional }
);
But it is seams not to be worked, may be some one may help?
You could have the following routes:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Blog",
"Blog/{id}",
new { controller = "Blog", action = "Show" }
);
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Blog", action = "Index", id = UrlParameter.Optional }
);
}
Now:
/Blog/123
will map toBlogController/Show(123)
action/
and/Blog
will map toBlogController/Index
action
The new route that you want to create should be the first one registered, otherwise the default routing will kick in and process the request.
Ensure that
routes.MapRouteLowercase(
"Blog",
"Blog/{id}",
new { controller = "Blog", action = "Show", id = UrlParameter.Optional }
);
is placed before
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
in Global.asax RegisterRoutes
精彩评论