asp.net-mvc - 为什么在 ASP.NET MVC 3 中,默认路由不适用于名为 "ContentController"的 Controller ?

标签 asp.net-mvc asp.net-mvc-3 routes

我有一个全新的 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/

相关文章:

javascript - 带 javascript 参数的 Laravel 路由

c# - 如何在 ASP.Net MVC 中将 RDLC 报告与 ReportViewer 控件一起使用?

asp.net - 虚拟目录中的相对路径

javascript - 在鼠标悬停在禁用的下拉菜单上时显示工具提示

c# - Entity Framework Include 和 Select 正在获取其他一些实体

Laravel - 我可以将带有 apiResource 的参数传递给 Controller ​​中的索引函数吗?

asp.net-mvc-3 - 在部署服务器中编辑 ASP.NET MVC 3 resx 文件,无需重新编译

asp.net-mvc-3 - ASP.NET AJAX.BeginForm 发送多个请求

asp.net-mvc-3 - 使用 Reveal jQuery 模态插件加载 MVC3 部分 View

web-services - 指定 Controller 的MapHttpRoute