提高ASP。NET MVC启动性能

时间:2022-06-01 13:03:59

I'm trying to improve the speed at which my MVC2 app is starting up.

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

I did a first round of performance sampling, and it appears that the

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

MvcAreaRegistration.RegisterAllAreas

is taking up most of the startup time.

占用了大部分的启动时间。

I read here that you can manually register the area's as well, and I would like to try that out, but I'm not sure how the syntax works on that page.

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

So my (first) question woud be: how can I register my Area's manually?

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

3 个解决方案

#1


3  

Try this super handy area registration utility. Not only does it make registration easier, but also way faster since it doesn't scan every loaded assembly for areas.

试试这个超级方便的区域注册工具。它不仅使注册变得更容易,而且更快,因为它不会扫描每一个装载的程序集。

#2


5  

First prepare yourself a helper method in Global.asax like this:

首先为自己准备一个全局帮助方法。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); 
}

Now you can use this helper method for manual registration in Application_Start like this:

现在您可以在Application_Start中使用这个助手方法进行手工注册,如下所示:

//Replace AreaRegistration.RegisterAllAreas(); with lines like those
RegisterArea<FirstAreaRegistration>(RouteTable.Routes, null); 
RegisterArea<SecondAreaRegistration>(RouteTable.Routes, null);

The AreaRegistration classes are created by Visual Studio when you add new Area, you can find them in Areas/AreaName directories.

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

#3


0  

You can do this completely by hand and avoid using RegisterArea implementations.

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

Check this article: http://www.philliphaydon.com/2011/07/mvc-areas-routes-order-of-routes-matter/

检查这篇文章:http://www.philliphaydon.com/2011/07/mvc-areas-routes-order-of-routes-matter/

In short - you need to add "area" DataToken to your route:

简而言之,您需要在您的路线上添加“区域”数据标记:

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");
}

#1


3  

Try this super handy area registration utility. Not only does it make registration easier, but also way faster since it doesn't scan every loaded assembly for areas.

试试这个超级方便的区域注册工具。它不仅使注册变得更容易,而且更快,因为它不会扫描每一个装载的程序集。

#2


5  

First prepare yourself a helper method in Global.asax like this:

首先为自己准备一个全局帮助方法。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); 
}

Now you can use this helper method for manual registration in Application_Start like this:

现在您可以在Application_Start中使用这个助手方法进行手工注册,如下所示:

//Replace AreaRegistration.RegisterAllAreas(); with lines like those
RegisterArea<FirstAreaRegistration>(RouteTable.Routes, null); 
RegisterArea<SecondAreaRegistration>(RouteTable.Routes, null);

The AreaRegistration classes are created by Visual Studio when you add new Area, you can find them in Areas/AreaName directories.

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

#3


0  

You can do this completely by hand and avoid using RegisterArea implementations.

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

Check this article: http://www.philliphaydon.com/2011/07/mvc-areas-routes-order-of-routes-matter/

检查这篇文章:http://www.philliphaydon.com/2011/07/mvc-areas-routes-order-of-routes-matter/

In short - you need to add "area" DataToken to your route:

简而言之,您需要在您的路线上添加“区域”数据标记:

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");
}