Writing a custom aspnet mvc action without an action
I'm looking to write a custom route that would allow the following
http://localhost/blog/tags/foo
Currently this is what actually works
http://localhost/tags/Index/nhibernate
I've tried the following with no success - any help开发者_Go百科 would be appreciated
routes.MapRoute(
"Tags",
"{controller}/{id}",
new { Controller = "Tags", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
"Tags",
"blog/{controller}/{id}",
new { Controller = "Tags", action = "Index", id = "" }
);
You could use in your global.asax something like this:
routes.MapRoute("Tags",
"blog/tags/{TagName}",
new { Controller = "Tags", action = "ShowTag", TagName = "" });
You'd then need a controller called 'TagsController.cs' with an ActionResult method called ShowTag plus a corresponding aspx called ShowTag.aspx. Your ShowTag method should look something like this:
public ActionResult ShowTag(string TagName)
{
//do stuff here to get Id from tag name and get other data etc...
return View();
}
Note that the order in which you map routes in the Global.asax.cs does matter.
精彩评论