为什么Asp.Net MVC没有在我的共享目录中搜索视图?

Kal*_*exx 2 asp.net-mvc views

在我的/Views/Shared/文件夹中,我创建了一个EntityNotFound.cshtml剃刀视图.在我的一个控制器操作中,我有以下调用:

return View(MVC.Shared.Views.EntityNotFound, "Company");
Run Code Online (Sandbox Code Playgroud)

这会导致以下异常:

System.InvalidOperationException:未找到视图'〜/ Views/Shared/EntityNotFound.cshtml'或其主文件或视图引擎不支持搜索的位置.搜索了以下位置:

〜/查看/公司/ Company.cshtml

〜/查看/公司/ Company.vbhtml

〜/查看/共享/ Company.cshtml

〜/查看/共享/ Company.vbhtml

我很困惑,因为它似乎甚至没有尝试搜索~/Views/Shared/EntityNotFound.cshtml.即使我更换MVC.Shared.Views.EntityNotFound"EntityNotFound"我得到同样的错误.

为什么Asp.Net MVC甚至没有尝试找到我的共享视图?

Ala*_*tts 5

看一下重载列表 View();

http://msdn.microsoft.com/en-us/library/system.web.mvc.controller.view.aspx

具体来说,当您传递View(string,string);它时,会看到第二个字符串作为主视图的名称.

可能发生的事情是,它无法找到"公司"主视图,你不会有异常消息说

......或者没找到它的主人......

这意味着它可能正在寻找NotFoundException.cshtml,但无法正确找到Company.cshtml它正在寻找的主人.