MVC3/Razor 错误:未找到视图或其主视图,或者没有视图引擎支持搜索的位置
我有一个名为“管理”的区域。 Visual Studio 中的文件夹结构是这样的:
Areas
Admin
Controllers
Views
我有一个名为 AccountController 的控制器,其中有一个名为“Verify”的操作。我在此操作中有这行代码:
return View("EmailVerificationComplete");
但是,我收到此错误:
未找到视图“EmailVerificationComplete”或其主视图,或者没有视图引擎支持搜索的位置。搜索了以下位置: 〜/视图/帐户/EmailVerificationComplete.aspx 〜/视图/帐户/EmailVerificationComplete.ascx 〜/视图/共享/EmailVerificationComplete.aspx 〜/视图/共享/EmailVerificationComplete.ascx 〜/Views/Account/EmailVerificationComplete.cshtml 〜/视图/帐户/EmailVerificationComplete.vbhtml 〜/Views/Shared/EmailVerificationComplete.cshtml ~/Views/Shared/EmailVerificationComplete.vbhtml
这是为什么?为什么 MVC 不知道查看相对于管理区域的 Views 文件夹?
我该如何解决这个问题?
谢谢,
萨钦
I have an Area called Admin. The folder structure in Visual Studio is this:
Areas
Admin
Controllers
Views
I have a controller called AccountController in which I have a Action called Verify. I have this line of code in this action:
return View("EmailVerificationComplete");
But, I get this error:
The view 'EmailVerificationComplete' or its master was not found or no view engine supports the searched locations. The following locations were searched:
~/Views/Account/EmailVerificationComplete.aspx
~/Views/Account/EmailVerificationComplete.ascx
~/Views/Shared/EmailVerificationComplete.aspx
~/Views/Shared/EmailVerificationComplete.ascx
~/Views/Account/EmailVerificationComplete.cshtml
~/Views/Account/EmailVerificationComplete.vbhtml
~/Views/Shared/EmailVerificationComplete.cshtml
~/Views/Shared/EmailVerificationComplete.vbhtml
Why is this? Why does MVC not know to look in the Views folder relative to the Admin area?
How do I solve this?
Thanks,
Sachin
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
因为您的
AccountController
不在Admin
区域内。它可能位于~/Controllers/AdminController.cs
中,因此按照惯例 ASP.NET MVC 将在~/Views/Shared
或~/Views/Account
中查找code> 获取其相应的视图。Because your
AccountController
is not inside theAdmin
area. It's probably in~/Controllers/AdminController.cs
so by convention ASP.NET MVC will look in~/Views/Shared
or~/Views/Account
for its corresponding views.我也有同样的问题。
我发现当你发送某个操作的请求时,即使 url 无效,mvc 也会尝试根据传入的 url 为你匹配操作。如果找到匹配项,(在我的情况下,它会转到正确的位置)控制器action方法),执行了,但是因为你传入的路径错误,所以无法找到关联的视图。
I had the same problem.
What I found was when you send a request for certain action, even if the url is invalid, mvc tries to match the action for you based on the url passed in. if a match is found, (in my case it goes to the correct controller action method), is executed, but because the path you passed in was wrong, it was not able to find the view associated.
对我来说,我试图将一个简单的字符串传递给视图。这导致 View() 渲染错误的重载,即 View(string viewName),但我真正想要的是 View(object model)。所以解决方案很简单,就是将字符串转换为对象。或者我可以使用 ViewBag 而不将任何内容传递给 View()。
For me, I was trying to pass in a simple string to the view. This was causing the View() to render the wrong overload which was View(string viewName), but what I really wanted was View(object model). So the solution was simply to cast the string to an object. Or I could have used the ViewBag and not pass anything to the View().
我之前遇到过这个错误...就我而言,.csproj 文件无意中被排除在 RELEASE 分支的合并之外,因此没有将丢失的文件索引到项目中。一旦 .csproj 被合并、重建并部署,就可以找到该文件。
I've run into this error before... In my case, it happened that the .csproj file was inadvertantly left out of the merge to the RELEASE branch and therefore didn't have the file that was missing indexed into the project. Once the .csproj was merged, rebuilt and deployed the file was able to be found.
使用
return View(模型:"EmailVerificationComplete");
use
return View(model:"EmailVerificationComplete");