Asp.net mvc 为什么Asp.NETMVC不在我的共享目录中搜索视图?

Asp.net mvc 为什么Asp.NETMVC不在我的共享目录中搜索视图?,asp.net-mvc,views,Asp.net Mvc,Views,在我的/Views/Shared/文件夹中,我创建了一个EntityNotFound.cshtmlrazor视图。在我的一个控制器操作中,我有以下调用: return View(MVC.Shared.Views.EntityNotFound, "Company"); 这会导致以下异常: System.InvalidOperationException:未找到视图“~/Views/Shared/EntityNotFound.cshtml”或其主视图,或者没有视图引擎支持搜索的

在我的
/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
。即使我将
MVC.Shared.Views.EntityNotFound
替换为
“EntityNotFound”
我也会得到同样的错误


为什么Asp.Net MVC甚至不尝试查找我的共享视图?

正确的语法应该是这样的(不要传递路径,MVC是一种按惯例使用的语言)

假设“Company”是要传递到视图的参数,请尝试如下操作:

  ViewBag.ErrorEntity = "Company";
  return View("EntityNotFound");
从这个角度看,

  <p>Entity not found: @ViewBag.ErrorEntity</p>
未找到实体:@ViewBag.ErrorEntity


查看
视图()的重载列表

具体来说,当您通过
视图(string,string)时它将第二个字符串视为主视图的名称

可能发生的情况是,它找不到“Company”主视图,您不会看到异常消息

。。。或者它的主人没有找到


这意味着它可能正在查找
NotFoundException.cshtml
,但无法正确地找到它正在寻找的作为主控的
Company.cshtml

啊,所以使用字符串模型是个坏主意:)实际上,您可以通过将字符串模型键入对象来欺骗系统。。。因此
视图(“NotFoundException”,(对象)“Company”)
  <p>Entity not found: @ViewBag.ErrorEntity</p>