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

我在视图中有一个小错误,并注意到视图引擎不仅搜索我的剃刀视图,还搜索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 

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

例如:

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

如果您希望在ViewEngines中使用自定义扩展,并且设置ViewEngine.FileExtensions = new[] { "cshtml" }也不适合您,请查看相关问题: Make ASP.NET MVC 3 Razor View Engine忽略.vbhtml文件

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