【问题标题】:Is there something special about a MVC controller called properties?称为属性的 MVC 控制器有什么特别之处吗?
【发布时间】:2016-03-19 17:48:47
【问题描述】:
在尝试解决这个问题多年后,考虑到路由冲突等等 - 我从一开始就开始了一个单独的项目。
当您尝试访问根站点 (http://site/properties) 时,看起来名为“属性”的 MVC 控制器总是返回 403.14 禁止消息 - 但是,其他页面可以正常工作 (http://site/properties/index)。
它作为一个区域的控制器可以正常工作,但是,我无法在主站点中创建它。
我想知道是否有人知道为什么以及最好的解决方法是什么?
【问题讨论】:
标签:
c#
asp.net-mvc
controller
asp.net-mvc-5
asp.net-mvc-routing
【解决方案1】:
除了 DavidG 的answer。
当您发布项目时,编译后的构建没有 Properties 文件夹。要解决本地开发时的问题,您可以将RouteExistingFiles 设置为true,以便 ASP.NET 路由处理所有请求。
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.RouteExistingFiles = true;
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
【解决方案2】:
问题是您的项目已经包含一个名为Properties 的文件夹,该文件夹主要用于AssemblyInfo.cs 文件,但其中也有其他内容。用于解析要发送到客户端的文件的引擎将文件和文件夹优先于路由。所以 URL http://site/properties 正在尝试从那里提供内容,但最终还是被阻止了。