布局页面“{path}”找不到
我们刚刚获得了一台新的 Windows 2008 R2 服务器,我正在尝试使用 Razor View Engine 在 IIS 7.5 中设置 MVC 3 站点。当我访问默认路由时,出现奇怪的错误。
布局页面 “~/Views/Shared/_Layout.cshtml”可以 在以下路径找不到: “~/Views/Shared/_Layout.cshtml”。
这很奇怪,因为我在 /views/home/index.cshtml
中明确设置了布局,如下所示:
@{
Layout = "~/Views/Shared/_Layout.cshtml";
}
我 100% 确定 /views/shared/_layout.cshtml
> 存在于文件系统中。这是一个屏幕截图。
如果我删除 Layout = "~/Views/Shared/_Layout.cshtml";< /code> 在我看来,页面将自行渲染
/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
We just got a new Windows 2008 R2 server and I am trying to set up an MVC 3 site in IIS 7.5 with the Razor View Engine. I am getting a strange error when I access the default route.
The layout page
"~/Views/Shared/_Layout.cshtml" could
not be found at the following path:
"~/Views/Shared/_Layout.cshtml".
This is strange because I explicitly set the Layout from within /views/home/index.cshtml
like so:
@{
Layout = "~/Views/Shared/_Layout.cshtml";
}
I am 100% certain that the /views/shared/_layout.cshtml
exists in the file system. Here's a screenshot.
If I remove the Layout = "~/Views/Shared/_Layout.cshtml";
from my view, the page will render out the contents of /home/index.cshtml
all by itself (without any of the code from _Layout.cshtml
). I have also tried setting the layout using _ViewStart.cshtml
.
Anyone know why MVC wouldn't be able to "find" the layout file? This website works perfectly fine when running in the built-in web server for visual studio.
Here's my stack trace:
[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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您似乎可能遇到权限问题。根据您提供的信息,这似乎是无法找到 _Layout.cshtml 文件的最可能原因。
在 Server 2008 和 IIS 7 中,应用程序池有自己的用于 ACL 的标识(名为 IIS AppPool{App Pool Name}),并且这些标识成为 IIS_IUSRS 组的一部分。此外,您的默认 inetpub/wwwroot 目录将具有 IIS_ISRS 组的读取权限。因此,如果您没有获得所需的权限,我认为应用程序池实际上并未以正确的身份运行。
身份可能不正确的原因之一是匿名身份验证用户身份的设置。进入站点(或整个服务器)的身份验证控件并编辑匿名身份验证规则以作为应用程序池身份运行。
这有点猜测,但似乎
It seems like you probably have a permissions issue. With the info you've given, it seems like the most likely cause of not being able to find the _Layout.cshtml file.
With Server 2008 and IIS 7, application pool's have their own identity used for ACL's (named IIS AppPool{App Pool Name}), and these identities are made part of the IIS_IUSRS group. Also, your default inetpub/wwwroot directory will have read permissions for the IIS_ISRS group. So, if your not getting the permissions you need, I would think that the App pool is not in fact running with the correct identity.
One reason the identity may be incorrect is the setting for Anonymous Authentication user identity. Go into the Authentication control for the site (or the whole server) and edit the Anonymous Authentication rule to run as the Application Pool Identity.
This is a bit of a guess, but it seemed this fellow had nearly the exact same issue.
正如 qbantek 发现的那样,对我来说,这与 _layout.cshtml 文件构建操作设置为 none 有关。
它设置为 none 的原因是我从一个空的 mvc 项目模板开始,并手动将 _layout.cshtml 文件添加到项目中。
因此吸取了教训,当您从空的项目模板开始并手动添加布局文件时,您还必须手动设置构建操作。
As qbantek found, for me this was related to the _layout.cshtml file build action being set to none.
the reason it was set to none is that I started with an empty mvc project template and manually added the _layout.cshtml file to the project.
so lesson learned, when you start with an empty project template, and you add the layout file manually, you have to set the build action manually as well.
这个问题有时可能与安全权限有关。
您确保有权读取/写入文件夹/文件。
This problem can be about security permission sometimes.
You make sure have permission to read/write in folder/file.
我的问题是 .suo 文件包含旧文件路径。我关闭了VS,删除了我的suo文件,重新打开VS,它工作正常。
My problem was the .suo file contained Old filepaths. I closed VS, deleted my suo file, re-opened VS, and it works fine.