403.13使用动作链接转到{controller} / Index

问题描述 投票:2回答:1

调试我的标题链接时出现以下错误。

Error Message

我尝试重新创建我的Index.cshtml(空和Razor / HTML)和我的ServicesController。如果我显式浏览到Services / Index,那么页面会正确加载。

IE:localhost:58069 / Services /不起作用但localhost:58069 / Services / Index

我的其他控制器使用相同的结构和ActionLink助手正常工作。

不工作的代码:

public class ServicesController : Controller
{
    // GET: Services
    public ActionResult Index()
    {
        return View();
    }
}

操作链接服务/索引的HTML帮助程序

<li>@Html.ActionLink("Services", "Index", "Services")</li>

工作守则

public class HomeController : Controller
{
    // GET: Home/Index
    public ActionResult Index()
    {
        return View();
    }
}

工作助手

<li>@Html.ActionLink("Welcome", "Index", "Home")</li>

路线配置

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

尝试了以下SO解决方案:

ASP MVC in IIS 7 results in: HTTP Error 403.14 - Forbidden

enter image description here

c# asp.net-mvc .net-4.5
1个回答
4
投票

此行为是由项目中的Services文件夹(在Scripts文件夹下)引起的。在IIS中托管时,不应该有与ASP.NET路由同名的文件或文件夹。

解决这个问题的方法:

  • routes.RouteExistingFiles = true;将让ASP.NET处理所有路由
  • 重命名您的服务文件夹或控制器
  • 将服务移动到单独的库并从Web项目中删除该文件夹

我建议使用后一个选项,因为设置RouteExistingFiles为true需要进行额外的研究以仔细检查你是不是打开了任何安全漏洞并且它清理了你的web项目(一点点分离永远不会伤害)。

© www.soinside.com 2019 - 2024. All rights reserved.