Can I change the search order for the physical views of an ASP.Net MVC 3 application

妖精的绣舞 提交于 2019-12-22 05:53:08

问题


I noticed Asp.net MVC 3 searches for .aspx files before .cshtml files. Can I change this search order? And how to do this?

Background information

When debugging I got the following exception:

The view 'Reset' or its master was not found. The following locations were searched:
~/Views/Demo/Reset.aspx
~/Views/Demo/Reset.ascx
~/Views/Shared/Reset.aspx
~/Views/Shared/Reset.ascx
~/Views/Demo/Reset.cshtml
~/Views/Demo/Reset.vbhtml
~/Views/Shared/Reset.cshtml
~/Views/Shared/Reset.vbhtml

I conclude from this, that the old .aspx views are searched for first.

Since I converted my site to MVC3, and all views to Razor, I would like the .cshtml files to be searched first. I think this would be better for performance.


回答1:


Yes. Change the order of the existing view engines..

But in a non-debug configuration, file locations are cached, so it will only help on the first lookup. I wouldn't sweat it.




回答2:


Thanks to the answer of Craig Stuntz I found the syntax I was looking for:

I added this to my Application_Start in Global.asax.cs:

    ViewEngines.Engines.Clear();
    ViewEngines.Engines.Add(new RazorViewEngine()); 
    // ViewEngines.Engines.Add(new WebFormViewEngine()); <-- uncomment if needed

This clears the registered ViewEngines, and adds them in the order I want.



来源:https://stackoverflow.com/questions/4198566/can-i-change-the-search-order-for-the-physical-views-of-an-asp-net-mvc-3-applica

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!