0

I have an asp.net mvc web application for a large scale application which is multi lingual supporting 15 different languages. I am using attribute routing to localize my routes as there is a requirement to localize the url's. This creates 2675 routes.

I use Html.ActionLink and Url.Action helper methods to generate url's to my controller actions. I have found though including these in my Partial Razor Views to slow down the rendering of my views. For example, if I remove some of them, the response time can from 5-6 seconds to under a second.

I believe the issue may be around the large number of routes. Therefore I believe optimization of this is required.

Has anyone have had this problem with routing with a large number of routes in conjunction with Attribute Routing? Any caching or similar that can be included to assist? I don't mean output caching, but caching of the routes to ensure Html.ActionLink and Url.Action render quicker?

Edit

With attribute routing, I have added route prefix attribute to my controllers and also attribute to my actions. Following is a sample of a class. I don't explicitly add routes anywhere that I am aware of - I am presuming that its attribute routing is doing so

[RoutePrefix("customer", TranslationKey = "customer")]
public partial class CustomerController : BaseController
{

[Route("logon", TranslationKey = "logon")]
public virtual ActionResult LogOn(string returnUrl = "")
{
...
}


[GET("changerepresentative/{id:long}")]
public virtual ActionResult ChangeRepresentative(long id)
{
...
}


[GET("profile", TranslationKey = "profile")]
public virtual ActionResult ProfileInformation()
{
...
}

// etc

}

Configuration for attribute routing

RouteTable.Routes.MapAttributeRoutes(config =>
                                           {
                                               var translationProvider = this.routeTranslationProvider.GetProvider();
                                               config.AddRoutesFromAssemblyOf<MvcApplication>();
                                               config.AddTranslationProvider(translationProvider);
                                               config.AppendTrailingSlash = true;
                                               config.UseLowercaseRoutes = true;
                                               config.InheritActionsFromBaseController = true;
                                               config.AutoGenerateRouteNames = true;
                                               config.RouteNameBuilder = RouteNameBuilders.FirstInWins;
                                           });
amateur
  • 43,371
  • 65
  • 192
  • 320
  • Are you measuring time of a debug-build in a debug environment, or a release-build on a production server? The two give very different results :) – iCollect.it Ltd Jun 17 '14 at 09:47
  • I measured a debug and release build on a development environment and both gave relatively similar results – amateur Jun 17 '14 at 12:43
  • 1
    Just reread your question. No wonder it is slow. You should not need 2675 routes under any circumstance (more than 5 seems excessive). `Html.ActionLink` etc have to search that table to ensure they produce the shortest possible URL (taking into account default values). Can you post an example of your custom URLs and a few custom routes so I might be able to suggest an alternative. (e.g. I only added one extra custom routing to handle 9 languages in our system). – iCollect.it Ltd Jun 17 '14 at 13:10
  • Thanks for your help - I have updated the question now. I am nowhere explicitly adding the 2675 routes, I believe that Attribute Routing is doing this. – amateur Jun 17 '14 at 13:35
  • You need to avoid that sort of situation. What is the "required" format of your URLs? I am puzzled why you need the attributes to begin with, so just trying to get my head around that first :) – iCollect.it Ltd Jun 17 '14 at 13:37
  • Maybe its a lack of understanding from me but I understood that I would need to specify TranslationKey in the AttributeRouting attribute? Or how should I setup routing for the actions? – amateur Jun 17 '14 at 13:46
  • Would you be happy with standard `domain.com/lang/controller/action/id` URLs where lang is an ISO code like `fr` or `fr-FR`? – iCollect.it Ltd Jun 17 '14 at 13:48
  • Unfortunately not, requirement is not to have any trace of lang or ISO code in the URL. – amateur Jun 17 '14 at 13:50
  • Unfortunate. You really are stuck between a rock and a hard place. You need to get the route count down or it will always be slow (or replace the routing handler, which is not trivial). There is a good reason sites use that standard (it is efficient). – iCollect.it Ltd Jun 17 '14 at 13:53
  • Yes from research, I understand. A chance is needed. However as an alternative I am looking to cache the routes by implementing a custom UrlHelper similar to what is outlined here - http://www.slideshare.net/rudib/aspnet-mvc-performance – amateur Jun 17 '14 at 14:00
  • 1
    Take care when reading a document 5 years old about performance. – Erik Philips Jun 17 '14 at 14:05
  • Thanks for the feedback - yes I am taking care but I have done a large amount of looking in to this and cant seem to find if the route caching has been introduced to the MVC framework as of yet – amateur Jun 17 '14 at 14:14

1 Answers1

0

I face a similar problem with our .NET MVC application. I've also not found any way to cache the route table. Until a better solution is found you can replace ActionLink with RouteLink which looks up the route based on the key so is much much faster.

Phil Hale
  • 3,453
  • 2
  • 36
  • 50