提高 ASP.NET MVC 启动性能

发布于 2024-10-20 05:34:42 字数 341 浏览 3 评论 0原文

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

我进行了第一轮性能采样,看起来它

MvcAreaRegistration.RegisterAllAreas

占用了大部分启动时间。

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

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

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

怼怹恏 2024-10-27 05:34:43

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

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

简而言之 - 您需要将“area”DataToken 添加到您的路线中:

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

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

Check this article:
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");
}
调妓 2024-10-27 05:34:42

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

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.

苯莒 2024-10-27 05:34:42

首先在 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); 
}

现在您可以使用此辅助方法在 Application_Start 中手动注册,如下所示:

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

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

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

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:

//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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文