我们刚刚获得了一台新的Windows 2008 R2服务器,我正在尝试使用Razor View Engine在IIS 7.5中设置MVC 3站点。当我访问默认路由时,我收到一个奇怪的错误。
布局页面 “〜/ Views / Shared / _Layout.cshtml”可以 在以下路径中找不到: “〜/查看/共享/ _Layout.cshtml”。
这很奇怪,因为我明确地在/views/home/index.cshtml
内设置了布局,如下所示:
@{
Layout = "~/Views/Shared/_Layout.cshtml";
}
我100%确定文件系统中存在/views/shared/_layout.cshtml
。这是一个截图。
如果我从我的视图中删除了Layout = "~/Views/Shared/_Layout.cshtml";
,该页面将自动呈现/home/index.cshtml
的所有内容(不包含_Layout.cshtml
中的任何代码)。我还尝试使用_ViewStart.cshtml
设置布局。
任何人都知道为什么MVC无法“找到”布局文件?在Visual Studio的内置Web服务器中运行时,该网站运行良好。
这是我的堆栈跟踪:
[HttpException (0x80004005): The layout page "~/Views/Shared/_Layout.cshtml" could not be found at the following path: "~/Views/Shared/_Layout.cshtml".]
System.Web.WebPages.WebPageExecutingBase.NormalizeLayoutPagePath(String layoutPage) +204956
System.Web.WebPages.WebPageBase.PopContext() +150
System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context) +384
System.Web.Mvc.<>c__DisplayClass1c.<InvokeActionResultWithFilters>b__19() +33
System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation) +784900
System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList`1 filters, ActionResult actionResult) +265
System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) +784976
System.Web.Mvc.Controller.ExecuteCore() +159
System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext) +335
System.Web.Mvc.<>c__DisplayClassb.<BeginProcessRequest>b__5() +62
System.Web.Mvc.Async.<>c__DisplayClass1.<MakeVoidDelegate>b__0() +20
System.Web.Mvc.<>c__DisplayClasse.<EndProcessRequest>b__d() +54
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +453
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +371
答案 0 :(得分:21)
好像你可能有权限问题。根据您提供的信息,这似乎是无法找到_Layout.cshtml文件的最可能原因。
对于Server 2008和IIS 7,应用程序池有自己的身份用于ACL(名为IIS AppPool {App Pool Name}),这些身份是IIS_IUSRS组的一部分。此外,您的默认inetpub / wwwroot目录将具有IIS_ISRS组的读取权限。因此,如果您没有获得所需的权限,我认为应用程序池实际上并没有以正确的身份运行。
身份可能不正确的一个原因是匿名身份验证用户身份的设置。进入站点(或整个服务器)的身份验证控制,并编辑匿名身份验证规则以作为应用程序池标识运行。
这是一个猜测,但似乎是this fellow had nearly the exact same issue。
答案 1 :(得分:2)
正如qbantek发现的,对我而言,这与_layout.cshtml文件构建操作设置为无关。
将其设置为none的原因是我从一个空的mvc项目模板开始并手动将_layout.cshtml文件添加到项目中。
所以吸取教训,当您开始使用空项目模板,并手动添加布局文件时,您还必须手动设置构建操作。
答案 2 :(得分:0)
我的问题是包含旧文件路径的.suo文件。我关闭VS,删除了我的suo文件,重新打开VS,它运行正常。