Asp.net mvc 提高ASP.NET MVC启动性能

Asp.net mvc 提高ASP.NET MVC启动性能,asp.net-mvc,performance,asp.net-mvc-2,Asp.net Mvc,Performance,Asp.net Mvc 2,我正在努力提高MVC2应用程序的启动速度 我做了第一轮性能抽样,结果显示 MvcAreaRegistration.RegisterAllAreas 正在占用大部分启动时间 我听说你也可以手动注册该区域,我想试试,但我不确定该页面的语法是如何工作的 所以我的(第一个)问题是:如何手动注册我的区域?首先在Global.asax中准备一个助手方法,如下所示: private static void RegisterArea<T>(RouteCollection routes, objec

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

我做了第一轮性能抽样,结果显示

MvcAreaRegistration.RegisterAllAreas
正在占用大部分启动时间

我听说你也可以手动注册该区域,我想试试,但我不确定该页面的语法是如何工作的


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

首先在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); 
}
//Replace AreaRegistration.RegisterAllAreas(); with lines like those
RegisterArea<FirstAreaRegistration>(RouteTable.Routes, null); 
RegisterArea<SecondAreaRegistration>(RouteTable.Routes, null);
private static void RegisterArea(路由收集路由,对象状态),其中T:AreaRegistration
{ 
AreaRegistration=(AreaRegistration)Activator.CreateInstance(typeof(T));
AreaRegistrationContext registrationContext=新的AreaRegistrationContext(registration.AreaName、routes、state);
string areaNamespace=registration.GetType().Namespace;
如果(!String.IsNullOrEmpty(areaNamespace))
registrationContext.Namespaces.Add(areaNamespace+“*”);
注册。注册区域(注册上下文);
}
现在,您可以使用此助手方法在应用程序中手动注册,如下所示:

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); 
}
//Replace AreaRegistration.RegisterAllAreas(); with lines like those
RegisterArea<FirstAreaRegistration>(RouteTable.Routes, null); 
RegisterArea<SecondAreaRegistration>(RouteTable.Routes, null);
//替换AreaRegistration.Registeralareas();用这样的线条
RegisterArea(RouteTable.Routes,空);
RegisterArea(RouteTable.Routes,空);

AreaRegistration类是由Visual Studio在添加新区域时创建的,您可以在Areas/AreaName目录中找到它们。

请尝试。它不仅使注册更容易,而且速度更快,因为它不会扫描每个加载的程序集的区域。

您可以完全手动完成此操作,并避免使用RegisterArea实现

检查这篇文章:

简言之,您需要将“区域”数据令牌添加到您的路由:

private void RegisterAreas(RouteCollection routes)
{
    // AreaRegistration.RegisterAllAreas();
    var route = routes.MapRoute(
        "MyArea_Default",
        "MyArea/{controller}/{action}/{id}",
        new { controller = "App", action = "Index", id = UrlParameter.Optional },
        new string[] { "MyProject.Areas.*" }
    ).DataTokens.Add("Area", "CDR");
}