MVC視圖引擎最佳化

來源:互聯網
上載者:User

標籤:style   blog   http   io   ar   color   使用   sp   for   

 

 

請首先看如下內容:

未找到視圖"Index"或其母片視圖,或沒有視圖引擎支援搜尋的位置。搜尋了以下位置
~/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

 

 

著作權歸二師弟和部落格園共同所有,歡迎轉載和分享知識。轉載請註明出處!

Mvc4和mvc3是這樣尋找的,其中*.aspx和*.ascx以及*.vbhtml都是我們不需要的。

我們可以想象,這裡不管採用什麼辦法,肯定至少4次訪問檔案系統並判斷對應檔案是否存在,才會找到我們的index.cshtml,這樣的開銷小型應用可以忽略,如果並發量千萬級(總之很大,具體未測)肯定對效能有一定影響。

怎麼避免,不要去尋找*.aspx,*.ascx以及*.vbhtml?

經過幾番折騰,我們成功找到瞭解決辦法:

Mvc中有兩個視圖引擎,webformengine和razorengine,解決辦法就是,確定不使用webform的情況下,移除webformengine,然後移除razorengine並添加改寫了的razorengine。

 

移除的代碼在global.ascx.cs的application_start方法中這樣寫:

ViewEngines.Engines.Clear();

重寫razorengine的方法:

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",

            };

        }

}

然後添加改寫的視圖引擎:

ViewEngines.Engines.Add(new Engine());

著作權歸二師弟和部落格園共同所有,歡迎轉載和分享知識。轉載請註明出處!

最終application_start方法如下:

MVC視圖引擎最佳化

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.