我有一个全新的 asp.net mvc 3 项目。我没有以任何方式修改路线。我有一个名为 PageController
的 Controller 和另一个 Controller 调用 ContentController
.
当我浏览到 domain.com/Page 时,页面 Controller 上的索引操作按预期执行并显示索引 View 。
当我浏览到 domain.com/Content 时,我收到 404 错误。如果我浏览到 domain.com/Content/Index 那么它工作正常。
如何解决这条单一路由的问题?
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
}
我尝试添加一条额外的路线:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
routes.MapRoute(
"Content", // Route name
"Content/{action}/{id}", // URL with parameters
new { controller = "Content", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
}
但是额外的路由并没有改变应用程序的行为。
什么可能导致这种情况?
最佳答案
这是因为有一个名为 content 的物理文件夹。拥有与物理文件夹同名的 Controller 可能会对您的网站产生一些不利影响。
关于asp.net-mvc - 为什么在 ASP.NET MVC 3 中,默认路由不适用于名为 "ContentController"的 Controller ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7586060/