MVC 3 Razor - How to stop the view engine from sea

2020-06-16 03:12发布

问题:

I had a small bug in a view and noticed that the view engine was searching not only for my razor views but for aspx/ascx pages. (My bug is fixed)

Is there a way to tell it to only search the Razor view engine?

Here is the error message that was displayed:

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

回答1:

You need to remove the WebFormsViewEngine from ViewEngine.Engines so that it only contains a RazorViewEngine.

For example:

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


回答2:

If you have a desire to use custom extensions in your ViewEngines, and the suggestion to set the ViewEngine.FileExtensions = new[] { "cshtml" } doesn't work for you either, check out the related question: Make ASP.NET MVC 3 Razor View Engine ignore .vbhtml files

Specifically my answer (or the others really) https://stackoverflow.com/a/20912185/1037948