在ASP.NET MVC中,访问网页是通过路由机制,路由通常是先访问控制器中的Action方法在通过Action访问相应的View中的代码,这其中需要找到相应名称的网页,搜索的时候不仅会搜索本来需要的网页也会搜索原本不需要的网页,比如说本来想访问
Index.cshtml 但是路由是Home/Index 框架会帮你搜索Views文件夹中Home子文件夹中的Index.cshtml或Index.vbhtml或者
Index.aspx,多多少少会影响到网站响应的速度,所以建议在Global.asax即网站开始Run的时候就将其他的不需要的搜索动作删除。
遵守配置原则现在App_Start文件夹下加一个类ViewEngineConfig.cs
public class ViewEngineConfig
{
public static void Register(ViewEngineCollection viewEngines)
{
viewEngines.Clear();
viewEngines.Add(new CSharpRazorViewEngine());
}
internal class CSharpRazorViewEngine : RazorViewEngine
{
public CSharpRazorViewEngine()
{
AreaViewLocationFormats = new[]
{
"~/Areas/{2}/Views/{1}/{0}.cshtml",
"~/Areas/{2}/Views/Shared/{0}.cshtml"
};
AreaMasterLocationFormats = new[]
{
"~/Areas/{2}/Views/{1}/{0}.cshtml",
"~/Areas/{2}/Views/Shared/{0}.cshtml"
};
AreaPartialViewLocationFormats = new[]
{
"~/Areas/{2}/Views/{1}/{0}.cshtml",
"~/Areas/{2}/Views/Shared/{0}.cshtml"
};
ViewLocationFormats = new[]
{
"~/Views/{1}/{0}.cshtml",
"~/Views/Shared/{0}.cshtml"
};
PartialViewLocationFormats = new[]
{
"~/Views/{1}/{0}.cshtml",
"~/Views/Shared/{0}.cshtml"
};
MasterLocationFormats = new[]
{
"~/Views/{1}/{0}.cshtml",
"~/Views/Shared/{0}.cshtml"
};
FileExtensions = new[]
{
"cshtml"
};
}
}
}
然后在
Global.asax中
protected void Application_Start()
{
ViewEngineConfig.Register(ViewEngines.Engines);//重新註冊引擎,只用razor視圖的cshtml
AreaRegistration.RegisterAllAreas();
WebApiConfig.Register(GlobalConfiguration.Configuration);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
Application["OnLineUserCount"] = 0;
}