請首先看如下內容:
未找到視圖"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方法如下:

