MVC View Engine Optimization

Source: Internet
Author: User

Please look at the following things first:

The View "Index" or its master view is not found , or there is no search location supported by the view engine. The following locations were searched :
~/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

Copyright belongs to the second brother and blog Garden Common All, welcome to reprint and share knowledge. Reprint please specify the source!

Mvc4 and MVC3 are found in this way, where *.aspx and *.ascx and *.vbhtml are what we don't need.

We can imagine that no matter what method is used here, must be at least 4 times to access the file system and determine whether the corresponding file exists, will find our index.cshtml, such overhead small applications can be ignored, if the concurrency of TENS (in short, very large, specific) must have a certain impact on performance.

How to avoid, not to find *.aspx,*.ascx and *.vbhtml?

After a few tossing, we succeeded in finding a solution:

There are two view engines in MVC, Webformengine and Razorengine, and the solution is to remove webformengine without using WebForm, Then remove the Razorengine and add the rewritten razorengine.

The removed code writes like this in the Application_Start method of Global.ascx.cs:

ViewEngines.Engines.Clear ();

To override the Razorengine method:

public class Engine:razorviewengine

{

Public Engine ()

{

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/{1}/{0}.cshtml",

"~/views/shared/{0}.cshtml",

};

Base. Fileextensions = new string[]

{

"Cshtml",

};

}

}

Then add the overridden view engine:

VIEWENGINES.ENGINES.ADD (New Engine ());

Copyright belongs to the second brother and blog Garden Common All, welcome to reprint and share knowledge. Reprint please specify the source!

The final Application_Start method is as follows:

MVC View Engine Optimization

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.