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

发布于 2024-12-08 09:31:54 字数 713 浏览 2 评论 0原文

在我的 /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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

混吃等死 2024-12-15 09:31:54

查看 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

... or its master was not found...

Which means that it is probably finding the NotFoundException.cshtml, but can't correctly find the Company.cshtml that it's looking for as the master.

兔小萌 2024-12-15 09:31:54

正确的语法应该是这样的(不要传递路径,MVC 是一种约定俗成的语言)

  return View("EntityNotFound");

假设“Company”是您想要传递给视图的参数,请尝试这样:

  ViewBag.ErrorEntity = "Company";
  return View("EntityNotFound");

从视图中

  <p>Entity not found: @ViewBag.ErrorEntity</p>

The correct syntax should be this (don't pass path, MVC is a language by conventions)

  return View("EntityNotFound");

Assuming "Company" is a param you wnat to pass to the view, try like this:

  ViewBag.ErrorEntity = "Company";
  return View("EntityNotFound");

And from the View

  <p>Entity not found: @ViewBag.ErrorEntity</p>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文