简体   繁体   English

MVC默认路由不适用于控制器

[英]MVC Default Route not Working for Controller

I've just started a new MVC project, and the default route doesn't seem to be working as I expect. 我刚刚开始一个新的MVC项目,默认路由似乎并没有按我预期的那样工作。

Routing Configuration: 路由配置:

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    routes.MapRoute(
        name: "Default",
        url: "{controller}/{action}/{id}",
        defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
    );
}

I have an ActionLink going to the "Index" action on the "Scripts" controller with @Html.ActionLink("Scripts", "Index", "Scripts") . 我有一个ActionLink ,它使用@Html.ActionLink("Scripts", "Index", "Scripts")转到“脚本”控制器上的“索引”操作。

public class ScriptsController : Controller
{
    public ActionResult Index()
    {
        var model = new IndexModel();
        .......
        return View(model);
    }
}

When clicking this link, the browser tries to navigate to " http://localhost:1733/Scripts/ ", and the browser displays a "Directory listing denied" error. 单击此链接时,浏览器尝试导航到“ http:// localhost:1733 / Scripts / ”,并且浏览器显示“目录列表被拒绝”错误。 When I manually change the URL to " http://localhost:1733/Scripts/Index " it works fine. 当我手动将URL更改为“ http:// localhost:1733 / Scripts / Index ”时,它可以正常工作。

Shouldn't the default route automatically infer the "Index" action? 默认路由是否应该自动推断“索引”操作? It does seem to be working for the ManageController class that was part of the default MVC site template (" http://localhost:1733/Manage " brings up the Index ActionResult of the ManageController ) 它似乎适用于默认MVC站点模板的一部分的ManageController类(“ http:// localhost:1733 / Manage ”调出ManageControllerIndex ActionResult)

What am I doing wrong? 我究竟做错了什么?

I guess you have a physical " Scripts " in your app root. 我猜您的应用程序根目录中有一个物理“ 脚本 ”。 The error message you see is the expected behavior. 您看到的错误消息是预期的行为。 When the GET request for /Scripts comes, IIS does not know whether to return the directory content (of Scripts) or have mvc return the ScriptsController result. 当对/Scripts的GET请求到达时,IIS不知道是返回目录(脚本的内容)还是让mvc返回ScriptsController结果。 It is confusing. 令人困惑。

You should try not to name your controller same as the physical directory name. 您应尽量不要将控制器的名称与物理目录名称相同。 Change either of the name. 更改任何一个名称。

If you do not want to rename either of those, you can set the RouteExistingFiles property to true to tell the framework whether routing should handle urls that match an existing physical file/directory. 如果您不想重命名这两个名称,则可以将RouteExistingFiles属性设置为true,以告诉框架路由是否应该处理与现有物理文件/目录匹配的url。

public class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
        routes.MapMvcAttributeRoutes();

        routes.MapRoute("Default", "{controller}/{action}/{id}",
             new { controller = "Home", action = "Index", id = UrlParameter.Optional });

        routes.RouteExistingFiles = true;
    }
}

You need to make sure that you do not have any action methods in the ScriptsController which matches the physical files/static resources (Ex : bundled/minimized script resource name). 您需要确保ScriptsController中没有与物理文件/静态资源匹配的任何操作方法(例如:捆绑/最小化的脚本资源名称)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM