Custom Routes to your Areas should be registered in your AreaRegistration class Not in Global.asx.cs
to add the route modify your NewsAreaRegistration
and add the new default route to your server after the area route. (the order is important here !)
Default routes should always be in the bottom of the routing table so that more specific routes can be matched.
Your routes in the RegisterArea
method should look something like this :
context.MapRoute(
"News_default",
"News/{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional }
);
context.MapRoute(
"DefaultStart",
"",
new { controller = "Articles", action = "IndexOfArticles"}
);
Update : Forgot to mention that you need to make sure you registered your areas in the Application_Start
method in the Global.asx.cs file.
AreaRegistration.RegisterAllAreas();
Update 2: How Routing Works In ASP.NET MVC
I understand your concern. Let me explain briefly how routing works.
In ASP.NET MVc there's one routing table that is used to match all url requests.
When you register a route in the Global.asx.cs or in AreaRegistration those routes are added to the routing table by the order they were registered by (that's why the orders is important and we should push our more specific routes to the top so they can get matched).
When it comes to Areas routes (those that are registered in the AreaRegistration
class). Those are always added to he top of the routing table before any of the routes registered in the Global.asx.cs file ( Areas routs are the first ones to be matched) which make since because otherwise you will miss match an Area called News for a Controller named News.
If you have more than one area, which Area's routes get checked first ? I'm not 100% sure, but by experiment i found out that they are ordered by the creation time, old areas comes first at the top of the routing table. ( it doesn't really matter because you won't have 2 areas with the same name)
Examples:
Suppose you have created the following areas. News
, Dashboard
, Api
and added the following route to your NewsAreaRegistration to match the root route as in your example above
context.MapRoute(
"DefaultStart",
"",
new { controller = "Articles", action = "IndexOfArticles"}
);
Your routing table will look something like this:
No Route Name URL Explanation
1 News_default News/{controller}/{action}/{id} Default For News Area
2 DefaultStart (empty) Root Route (match root url)
3 Dashboard_default Dashboard/{controller}/{action}/{id} Default For Dashboard Area
4 Api_default Api/{controller}/{action}/{id} Default For Api Area
5 Default {controller}/{action}/{id} Default (No Areas)
Now when you application receives a request. It will go through the routes one by one and look for a match. In our case when your request the root url. The second route will be matched. and because we set the default values for controller = "Articles" and action ="IndexOfArticles" the request will be redirected accordingly.
Hope that was helpful.