Ber*_*van 13 asp.net-mvc performance asp.net-mvc-2
我正在努力提高我的MVC2应用程序启动的速度.
我做了第一轮性能抽样,看起来就是
MvcAreaRegistration.RegisterAllAreas
Run Code Online (Sandbox Code Playgroud)
占据了大部分的启动时间.
我在这里读到你也可以手动注册该区域,我想尝试一下,但我不确定该语法在该页面上是如何工作的.
所以我的(第一个)问题是:我如何手动注册我的区域?
首先在Global.asax中为自己准备一个帮助方法,如下所示:
private static void RegisterArea<T>(RouteCollection routes, object state) where T : AreaRegistration
{
AreaRegistration registration = (AreaRegistration)Activator.CreateInstance(typeof(T));
AreaRegistrationContext registrationContext = new AreaRegistrationContext(registration.AreaName, routes, state);
string areaNamespace = registration.GetType().Namespace;
if (!String.IsNullOrEmpty(areaNamespace))
registrationContext.Namespaces.Add(areaNamespace + ".*");
registration.RegisterArea(registrationContext);
}
Run Code Online (Sandbox Code Playgroud)
现在,您可以在Application_Start中使用此帮助程序方法进行手动注册,如下所示:
//Replace AreaRegistration.RegisterAllAreas(); with lines like those
RegisterArea<FirstAreaRegistration>(RouteTable.Routes, null);
RegisterArea<SecondAreaRegistration>(RouteTable.Routes, null);
Run Code Online (Sandbox Code Playgroud)
添加新区域时,Visual Studio会创建AreaRegistration类,您可以在Areas/AreaName目录中找到它们.