为什么 Asp.Net MVC 不在我的共享目录中搜索视图?
在我的 /Views/Shared/
文件夹中,我创建了一个 EntityNotFound.cshtml
razor 视图。在我的控制器操作之一中,我有以下调用:
return View(MVC.Shared.Views.EntityNotFound, "Company");
这会导致以下异常:
System.InvalidOperationException:未找到视图“~/Views/Shared/EntityNotFound.cshtml”或其主视图,或者没有视图引擎支持搜索的位置。搜索了以下位置:
~/Views/Company/Company.cshtml
~/Views/Company/Company.vbhtml
~/Views/Shared/Company.cshtml
~/Views/Shared/Company.vbhtml
我很困惑,因为它不甚至似乎在尝试搜索~/Views/Shared/EntityNotFound.cshtml
。即使我用 "EntityNotFound"
替换 MVC.Shared.Views.EntityNotFound
,我也会得到同样的错误。
为什么 Asp.Net MVC 甚至不尝试找到我的共享视图?
In my /Views/Shared/
folder I created an EntityNotFound.cshtml
razor view. In one of my controller actions I have the following call:
return View(MVC.Shared.Views.EntityNotFound, "Company");
This causes the following exception:
System.InvalidOperationException: The view '~/Views/Shared/EntityNotFound.cshtml' or its master was not found or no view engine supports the searched locations. The following locations were searched:
~/Views/Company/Company.cshtml
~/Views/Company/Company.vbhtml
~/Views/Shared/Company.cshtml
~/Views/Shared/Company.vbhtml
I am confused, because it does not even seem to be attempting to search ~/Views/Shared/EntityNotFound.cshtml
. Even if I replace MVC.Shared.Views.EntityNotFound
with "EntityNotFound"
I get the same error.
Why is Asp.Net MVC not even attempting to find my shared view?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
查看
View();
的重载列表http://msdn.microsoft.com/en-us/library/system.web.mvc.controller.view.aspx
具体来说,当您传递
View(字符串,字符串);
它将第二个字符串视为主视图的名称。可能发生的情况是,它找不到“公司”主视图,您不会看到异常消息
这意味着它可能正在找到
NotFoundException.cshtml
,但无法正确找到它所在的Company.cshtml
寻找主人。Have a look at the list of overloads for
View();
http://msdn.microsoft.com/en-us/library/system.web.mvc.controller.view.aspx
Specifically, when you pass
View(string,string);
it sees the second string as the name of the master view.Whats probably happening, is that it can't find the "Company" master view, you'll not the exception messages says
Which means that it is probably finding the
NotFoundException.cshtml
, but can't correctly find theCompany.cshtml
that it's looking for as the master.正确的语法应该是这样的(不要传递路径,MVC 是一种约定俗成的语言)
假设“Company”是您想要传递给视图的参数,请尝试这样:
从视图中
The correct syntax should be this (don't pass path, MVC is a language by conventions)
Assuming "Company" is a param you wnat to pass to the view, try like this:
And from the View