一个 ASP.NET MVC 路由不起作用

One ASP.NET MVC route not working

我有路由问题,RouteConfig.cs 包含这些路由:

routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

routes.MapRoute(
    "TermsOfService",
    "termsofservice",
    new { controller = "Home", action = "TermsOfService" }
);

routes.MapRoute(
    "PrivacyPolicy",
    "privacypolicy",
    new { controller = "Home", action = "PrivacyPolicy" }
);

routes.MapRoute(
    "Contact",
    "contact",
    new { controller = "Home", action = "Contact" }
);

routes.MapRoute(
    "Support",
    "support",
    new { controller = "Home", action = "Support" }
);

routes.MapRoute(
    "ReadOurStory",
    "readourstory",
    new { controller = "Home", action = "ReadOurStory" }
);

routes.MapRoute(
    name: "BlogItem",
    url: "blog/{name}",
    defaults: new { controller = "Home", action = "BlogItem" }
);

routes.MapRoute(
    name: "ProductDetail",
    url: "products/{name}",
    defaults: new { controller = "Home", action = "Product" }
);

routes.MapRoute(
    name: "Products",
    url: "products",
    defaults: new { controller = "Home", action = "Products" }
);

routes.MapRoute(
    name: "Tutorials",
    url: "tutorials",
    defaults: new { controller = "Home", action = "Tutorials" }
);

routes.MapRoute(
    name: "Blog",
    url: "blog",
    defaults: new { controller = "Home", action = "Blog" }
);

routes.MapRoute(
    name: "TutorialDetail",
    url: "tutorials/{name}",
    defaults: new { controller = "Home", action = "Tutorial" }
);

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

以及 HomeController 中的这两个操作:

public ActionResult Products()
{
    var model = LoadItemsModel(ItemType.Product);
    return View(model);
}

public ActionResult Tutorials()
{
    var model = LoadItemsModel(ItemType.Tutorial);
    return View(model);
}

现在产品的 link 正在运行:

http://localhost:61296/products/

但是 link 教程:

http://localhost:61296/tutorials/

returns错误

HTTP 错误 403.14 - 禁止

Web 服务器配置为不列出此目录的内容

我试图将仅用于测试的路由更改为 tutorials2,并将操作更改为 Tutorials2,然后这个修改后的 link 正在运行。我不知道为什么路由 tutorials 不起作用。

根据错误,我猜测您的项目文件夹中有一个名为 "tutorials" 的物理目录。任何物理 files/directories 将始终否决任何路由。然后,由于默认情况下目录列表是禁用的,您会收到 403 错误。删除或重命名物理目录,你应该没问题。