asp mvc4 html 助手没有导航到预期的页面

asp mvc4 html helper not navigating to expected page

我正在使用以下代码生成 link

@Html.ActionLink("Search", "Index", "Properties")

在客户端呈现

<a href="/Properties">Search</a>

看了上面的 link 我预计会

controller: Properties

action: Index

当我单击 link 时,我被导航到 http://localhost:49878/Properties/ 并且我收到错误消息

HTTP Error 403.14 - Forbidden The Web server is configured to not list the contents of this directory.

如果我导航到 http://localhost:49878/Properties/Index,我会得到预期的页面。

为什么?

我查看了 RouteConfig 以尝试理解这一点并查看默认路由

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

我以为下面的方法可以解决问题,但没有,为什么?

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

您不能使用属性,因为它是具有 AssemblyInfo.cs 的文件夹的名称。这么受限的名字。

将此添加到

routes.RouteExistingFiles = true; 

在 RouteConfig 中注册路由以忽略物理路径(虽然这对静态文件会有一些复杂性,但请彻底测试)。

更好的方法是重命名您的控制器并使用它,但不确定是否还有其他可以做的事情。

您在您的解决方案中只会看到一个 Properties 文件夹,如下所示:

但是如果像这样打开项目文件夹:

您可以在两个路径中看到两个文件夹:

根目录

查看文件夹

路由系统检查文件系统以查看 URL 是否与磁盘上的 file/folder 匹配。如果找到匹配项,路由将被忽略并且请求绕过任何路由条目,以便直接提供文件。

要解决此问题,您可以在 RouteConfig:

中将 RouteExistingFiles 属性 设置为 true
routes.RouteExistingFiles = true;