MVC 3 Razor——如何阻止视图引擎搜索aspx和ascx页面

本文关键字:aspx 搜索 引擎 ascx 页面 视图 Razor 何阻止 MVC | 更新日期: 2023-09-27 18:10:52

我在一个视图中有一个小错误,我注意到视图引擎不仅搜索我的razor视图,还搜索aspx/ascx页面。(我的错误已修复)

有没有办法告诉它只搜索Razor视图引擎?

下面是显示的错误信息:

The view 'Index' or its master was not found or no view engine supports the searched locations. The following locations were searched:
~/Areas/BO/Views/Organization/Index.aspx
~/Areas/BO/Views/Organization/Index.ascx
~/Areas/BO/Views/Shared/Index.aspx
~/Areas/BO/Views/Shared/Index.ascx
~/Views/Organization/Index.aspx
~/Views/Organization/Index.ascx
~/Views/Shared/Index.aspx
~/Views/Shared/Index.ascx
~/Areas/BO/Views/Organization/Index.cshtml
~/Areas/BO/Views/Organization/Index.vbhtml
~/Areas/BO/Views/Shared/Index.cshtml
~/Areas/BO/Views/Shared/Index.vbhtml
~/Views/Organization/Index.cshtml
~/Views/Organization/Index.vbhtml
~/Views/Shared/Index.cshtml
~/Views/Shared/Index.vbhtml

MVC 3 Razor——如何阻止视图引擎搜索aspx和ascx页面

您需要从ViewEngine.Engines中删除WebFormsViewEngine,以便它只包含RazorViewEngine

例如:

ViewEngines.Engines.Clear();
ViewEngines.Engines.Add(new RazorViewEngine());

如果您希望在viewengine中使用自定义扩展,并且设置ViewEngine.FileExtensions = new[] { "cshtml" }的建议也不适合您,请查看相关问题:制作ASP. js。. MVC 3 Razor视图引擎忽略。vbhtml文件

特别是我的答案(或其他人真的)https://stackoverflow.com/a/20912185/1037948