asp.net-mvc 将 Index 设置为控制器的默认路由
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5252925/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Setting up Index as the default route for a controller
提问by Chris S
I have a url
我有一个网址
which I want to turn into
我想变成
That could also be something like http://www.roadkillwiki.org/Page/my-url-with-spaces- the parameter is a string. The route setup I've tried is:
这也可能类似于http://www.roadkillwiki.org/Page/my-url-with-spaces- 参数是一个字符串。我试过的路线设置是:
routes.MapRoute(
"ControllerDefault",
"{controller}/{id}",
new { controller = "Page", action = "Index", id = UrlParameter.Optional }
);
However this is interfering with the default "id" route that MVC projects come with. Is there any way of achieving this?
但是,这会干扰 MVC 项目附带的默认“id”路由。有没有办法实现这一目标?
回答by Daniel Liuzzi
You don't need to lose the default route. The key to avoiding your routes interfere with each other is to order them so the more specific rules precede the less specific ones. For example:
您不需要丢失默认路由。避免您的路线相互干扰的关键是对它们进行排序,以便更具体的规则先于不太具体的规则。例如:
// Your specialized route
routes.MapRoute(
"Page",
"Page/{slug}",
new { controller = "Page", action = "Index" }
);
// Default MVC route (fallback)
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
Then your PageController would look like this:
然后你的 PageController 看起来像这样:
using System.Web.Mvc;
public class PageController : Controller
{
public string Index(string slug)
{
// find page by slug
}
}
That said, I would stronglyadvice you to do this instead:
也就是说,我强烈建议你这样做:
// Your specialized route
routes.MapRoute(
"Page",
"Page/{id}/{slug}",
new { controller = "Page", action = "Index", slug = UrlParameter.Optional }
);
// MVC's default route (fallback)
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
And your PageController:
还有你的 PageController:
using System.Web.Mvc;
public class PageController : Controller
{
public string Index(int id)
{
// find page by ID
}
}
By including the page ID either at the beginning of your URL (like StackOverflow does) or at the end, you can then just ignore the slug, and instead retrieve your pages by ID. This will save you a ton of headaches if your users change the page name. I have gone through this and it's painful; you basically have to keep a record of all names your pages have had in the past, just so your visitors/search engines don't get a 404 every time a page is renamed.
通过在 URL 的开头(如 StackOverflow 所做的)或末尾包含页面 ID,您可以忽略 slug,而是通过 ID 检索您的页面。如果您的用户更改页面名称,这将为您省去很多麻烦。我经历了这一切,这很痛苦;您基本上必须记录您的页面过去的所有名称,这样您的访问者/搜索引擎就不会在每次重命名页面时收到 404。
Hope this helps.
希望这可以帮助。
回答by frennky
If you don't need a default route that came with project template you can set up one like this:
如果您不需要项目模板附带的默认路由,您可以像这样设置一个:
routes.MapRoute(
"ControllerDefault",
"{controller}/{pagename}",
new { controller = "Page", action = "Index" }
);
And than in your controller you would have an action:
比在你的控制器中你会有一个动作:
public ActionResult Index(string pagename)
{
//do something
}