我正在将 ASP.net MVC4 与 VWD Express 2010 一起使用。我已经设置了一个额外的路由来/Home/
从 URL中删除无关内容,如此处和此处所建议的那样。我确保将它们按正确的顺序排列:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Home",
url: "{action}",
defaults: new { controller = "Home", action = "Index" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
就删除/Home/
而言,这有效,但现在我在尝试访问非默认控制器时得到 404:
Server Error in '/' Application.
The resource cannot be found.
Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly.
Requested URL: /LogItem/
我已经将它追踪到路线,因为如果我删除非默认的,它会再次正常工作。但是,我不确定自己做错了什么,并且根据我对其他答案和文档的理解,这应该可行。
编辑:由于我的描述显然不清楚,我添加了“主页”路由以能够链接到/About/
而不是/Home/About
等。这是我在谷歌搜索后找到的建议解决方案。我基本上希望它与HomeController
使用速记 URL ( /{action}
) 以及我还完整添加的任何其他控制器( /{controller}/{action}/{optional id}
)实现的操作相匹配