提高ASP.NET MVC启动性能

Ber*_*van 13 asp.net-mvc performance asp.net-mvc-2

我正在努力提高我的MVC2应用程序启动的速度.

我做了第一轮性能抽样,看起来就是

MvcAreaRegistration.RegisterAllAreas
Run Code Online (Sandbox Code Playgroud)

占据了大部分的启动时间.

在这里读到你也可以手动注册该区域,我想尝试一下,但我不确定该语法在该页面上是如何工作的.

所以我的(第一个)问题是:我如何手动注册我的区域?

tpe*_*zek 5

首先在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目录中找到它们.


Bro*_*ook 5

试试这个超级方便的区域注册实用程序。它不仅使注册更容易,而且速度更快,因为它不会扫描每个加载的程序集的区域。