.Net MVC Area Registration Sequence

.Net MVC has the ability to create areas to better organize code. It is well documented on MSDN how to create Areas and how to configure routing for Areas. In the application startup code, you can call AreaRegistration.RegisterAllAreas() to register all areas. This needs a routing cs file for each area as described in the preceding article.

Once registered, all the areas will start showing up in the Routing Table. The more the number of areas, the greater the depth of the routing table. You can use the Route Debugger to see your application's routing table and how incoming URLs are matched before passing to the controller. The deeper the table the more time it would take the table to match since MVC matches in sequential order in the routing table.

It is advisable to put the routes that are most used at the top of the routing table. But RegisterAllAreas does not allow the definition of the order of registration or allow the sorting of the routing. It is not clear how Register All Area sequences are registered and your most-wanted route might end up at the bottom of the routing table. There is a way to customize the sequence handling of the compile sequence.
Let's say you have two areas transactions and a workflow as shown below and each has its own registration cs file.



Here is the sample registration cs file code for transactions. Similarly one would exist for the workflow area. You can see the second method actually register the route.

  1. public override string AreaName   
  2. {  
  3.     get   
  4.     {  
  5.         return "Transactions";  
  6.     }  
  7. }  
  8. public override void RegisterArea(AreaRegistrationContext context)  
  9. {  
  10.     context.MapRoute  
  11.     (  
  12.         "Transactions_default",  
  13.         "Transactions/{controller}/{action}/{id}",  
  14.     new   
  15.     {  
  16.         action = "Index", id = UrlParameter.Optional  
  17.     }  
  18.     );  
  19. }  
If you run the Route debugger, the routing table is displayed as in the following. I had many other areas too and as you can see the workflow ended up at the bottom of the table.

routing table

Now if the workflow is the most-used route then the route mapping will match against all the others that are above it and then finally matches to the workflow. If you want to move it up then open the the .csproj file and look for "<compile include=". This is the area in the project file that lists the sequence in which cs files should be compiled and the order of the compile. Here is the extract from mine.

  1. <Compile Include="Areas\Transaction\TransactionAreaRegistration.cs" />  
  2. <Compile Include="Areas\Profiles\ProfilesAreaRegistration.cs" />  
  3. <Compile Include="Areas\Agreement\AgreementAreaRegistration.cs" />  
Now to define the sequence of the routing table, all I must do is rearrange this list and move the workflow registration file to the top. So my new csproj would look as in the following.
  1. <Compile Include="Areas\Workflow\WorkflowAreaRegistration.cs" />  
  2. <Compile Include="Areas\Transaction\TransactionAreaRegistration.cs" />  
  3. <Compile Include="Areas\Profiles\ProfilesAreaRegistration.cs" />  
  4. <Compile Include="Areas\Agreement\AgreementAreaRegistration.cs" />  
Now re-run the application and look at the routing table and you will see that the workflow is at the top.

rerun application

That's it, now you can define the sequence of the area registrations and thus the sequence in the routing table.


Similar Articles