I often use the ASP. NET MVC framework to do Web sites. In general, the MVC framework is a very good framework. I personally feel that the division of labor is more reasonable and clearer than the Web form pattern of the past, but the work to developers is also a lot more.
When using the standard configuration in the new controller, not yet built the view, run the site, access to this we can see
So we can judge that the default view engine loads first in the order shown in the
~/views/home/index.aspx
~/views/home/index.ascx
~/views/shared/index.aspx
~/views/shared/index.ascx
~/views/home/index.cshtml
~/views/home/index.vbhtml
~/views/shared/index.cshtml
~/views/shared/index.vbhtml
It is said that every time you need to search so many times to load the cshtml files I need, the overall feeling is not very good, although the impact on performance is relatively small, but also through the cache technology, to solve the problem of frequently viewed page speed. But after all it is a question or learn how to solve the good.
The default MVC view engine loads the WebForm view engine and the Razor view engine.
We just need to empty the original engine and join the Razor view engine at the beginning of the server, when the Application_Start event occurs.
In the Global.asax.cs file
protected void Application_Start () { arearegistration.registerallareas (); Routeconfig.registerroutes (routetable.routes); ViewEngines.Engines.Clear ();//Clear the original engine VIEWENGINES.ENGINES.ADD (new razorviewengine ());// Join Razor engine }
After joining the Razor engine
In fact, I think this is possible, directly in the corresponding folder first search cshtml
But I always have to look at his engine, but I'm just looking at him. View query reads, parsed by using. NET Reflector, the code for the Razor view engine was the same.
Public Razorviewengine (Iviewpageactivator viewpageactivator):Base(viewpageactivator) {Base. Areaviewlocationformats =New string[] {"~/areas/{2}/views/{1}/{0}.cshtml","~/areas/{2}/views/{1}/{0}.vbhtml","~/areas/{2}/views/shared/{0}.cshtml","~/areas/{2}/views/shared/{0}.vbhtml" }; Base. Areamasterlocationformats =New string[] {"~/areas/{2}/views/{1}/{0}.cshtml","~/areas/{2}/views/{1}/{0}.vbhtml","~/areas/{2}/views/shared/{0}.cshtml","~/areas/{2}/views/shared/{0}.vbhtml" }; Base. Areapartialviewlocationformats =New string[] {"~/areas/{2}/views/{1}/{0}.cshtml","~/areas/{2}/views/{1}/{0}.vbhtml","~/areas/{2}/views/shared/{0}.cshtml","~/areas/{2}/views/shared/{0}.vbhtml" }; Base. Viewlocationformats =New string[] {"~/views/{1}/{0}.cshtml","~/views/{1}/{0}.vbhtml","~/views/shared/{0}.cshtml","~/views/shared/{0}.vbhtml" }; Base. Masterlocationformats =New string[] {"~/views/{1}/{0}.cshtml","~/views/{1}/{0}.vbhtml","~/views/shared/{0}.cshtml","~/views/shared/{0}.vbhtml" }; Base. Partialviewlocationformats =New string[] {"~/views/{1}/{0}.cshtml","~/views/{1}/{0}.vbhtml","~/views/shared/{0}.cshtml","~/views/shared/{0}.vbhtml" }; Base. Fileextensions =New string[] {"cshtml","vbhtml" }; }
So we can inherit razorviewengine. Customize the location and order of engine searches
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingsystem.web;usingSYSTEM.WEB.MVC;namespacealltest.app_start{ Public classMyviewengines:razorviewengine { PublicMyviewengines () {Base. Areaviewlocationformats =New string[] { "~/areas/{2}/views/{1}/{0}.cshtml", "~/areas/{2}/views/shared/{0}.cshtml", }; Base. Areamasterlocationformats =New string[] { "~/areas/{2}/views/{1}/{0}.cshtml", "~/areas/{2}/views/shared/{0}.cshtml", }; Base. Areapartialviewlocationformats =New string[] { "~/areas/{2}/views/{1}/{0}.cshtml", "~/areas/{2}/views/shared/{0}.cshtml", }; Base. Viewlocationformats =New string[] { "~/views/{1}/{0}.cshtml", "~/views/shared/{0}.cshtml", }; Base. Masterlocationformats =New string[] { "~/views/{1}/{0}.cshtml", "~/views/shared/{0}.cshtml", }; Base. Partialviewlocationformats =New string[] { "~/views/shared/{0}.cshtml", "~/views/{1}/{0}.cshtml", }; Base. Fileextensions =New string[] { "cshtml", }; } }}
And then when the server starts up, use this own engine to estimate that it will do a better job.
protected void Application_Start () { arearegistration.registerallareas (); Routeconfig.registerroutes (routetable.routes); // ViewEngines.Engines.Clear (); ViewEngines.Engines.Add (new myviewengines ()); }
ASP. NET MVC optimizations for the view engine