无需在 URL 中指定操作名称即可使路由生效。 -MVC

Make route work without specifying action name in URL. - MVC

我的 MVC 项目中有以下控制器:

public class PressController : Controller
    {
        // GET: Press
        public ActionResult Index()
        {
            return File("../press/FFF_PRESS.zip", ".zip");
        }
    }

我的路线

 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 }
        );
    }

当我加载网站时,我的 URL 如下:

www.example.com

当我点击下面的Action<li class="footer__navEl"><a href='@Url.Action("Index", "Press")'>PRESS</a></li>

时正确显示主页

我希望 URL 成为

www.example.com/press 

和 return 压缩文件。

但是当我点击这个 Action 我得到以下信息:

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

然而当我指定

www.example.com/press/index

.zip 文件return编辑正确。

现在我在 routes.config 中添加了以下内容:

routes.MapRoute("Press", "Press", new { controller = "Press", action = "Index" });

我仍然遇到上面提到的相同错误,有人可以阐明我可能缺少的内容以使其正常执行吗?

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

该错误表明您的 Web 服务器上有一个名为 press物理 目录。发生的情况是 Web 服务器正在返回目录,而不是将请求传递给 MVC。 IIS 中的默认设置是不列出目录的内容,因此出现错误。

您需要删除 press 目录(推荐),或者使用 runAllManagedModulesForAllRequests 将 IIS 重新配置为 运行 MVC 模块而不是目录,[=14] =].

这根本不是路由问题 - 这是网络服务器配置问题。