views:

49

answers:

2

Hello,

How and where do I configure my application so that, when it launches, the action (thus the page to be displayed) be, not in the root structure, but rather in a given area of my choice?

Let's say, Action = "IndexOfArticles", Controller = "Articles", Area = "News". I want this setting to be the default when I launch the application.

I've worked on the NewsAreaRegistration class and set up the above configuration. now I suspect that, to make it work, I need to do something with Global.asx.cs as well, but I don't know what to do.

EDIT

This is what I mean

routes.MapRoute(
            "Default", // Route name
            "{controller}/{action}/{id}", // URL with parameters
            new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
        );

The above code, for instance, will cause the application to start with Index action located in Home controller to execute. That's not what I want. Thanks for helping.

+2  A: 

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.

Manaf Abu.Rous
@Manaf: can u register the above routes as an example?
Richard77
@Manaf: Thanks, it's working. Does "DefaultStart" have something to do with the result? Or I could have called it something else?
Richard77
@Richard77: "DefaultStart" is just a name you can name it whatever you what just make sure it won't collide with other routes names.
Manaf Abu.Rous
@Manaf: I'd like to know what made the routing framework to choose particularly this route over thousands other? Without area, it easy. The routing module has only one place to look. It starts from the top to the bottom. But, when you add several areas, how does it know to go look for a default route in a particular area??? Your above configuration has worked, but I still don't know why. You said, it's not because of the "DefaultStartName". Is it "" that made the trick? Sorry to insist, but everything will fall back when I understand this.
Richard77
So, if I've understood: will the match be possible because of the "" which corresponds to "/" of the default routing?
Richard77
@Richard77: Yes.
Manaf Abu.Rous
Fantastic edit Manaf :)
Stephen M. Redd
+2  A: 

Considering the nature of the questions you've been asking, I want to recommend a resource that I think you'll REALLY love. Steven Sanderson's book "Pro ASP.NET MVC 2 Framework (second edition)" by apress. Chapter 8 in particular goes into a LOT of detail about routing and areas. The book itself is excellent and thourough; one of the best examples of dev/programming books I've ever read. It is available in Kindle form, and if you order from apress directly you can get it in PDF e-book form. It is of course available in paper form too:

Amazon Link

or

Direct from Apress

The specific thing you might want to understand about areas are that they are based on namespaces. When you create an area, the namespace of that area is how MVC locates them and decides what is "in" and area and what isn't. Generally this means that routing to areas is done via the URLs that have the area's name as the first folder in the URL, but this maps to the controller's namespace, not necessarily the physical folder names (though they should match if you want to preserve your sanity).

The route in Manaf's example is working "by accident" more than by intent, which is probably why you aren't quite getting what it does. There is a quirk with ambiguous controller names in the "root area" (that is, the controller's folder that isn't in an area). For a route leads to the root area, but it can't find a controller there, it will scan all areas looking for a match. In this case it finds a match in your news namespace, and works. But this scanning only works if there is only one controller that matches. So if you were to create another controller in another area with the same name, it will fail with a "multiple types found" kind of exception.

You have two good ways to make this work more reliably. Either by prioritizing the news area in the routes, or by redirection:

Redirection:

Instead of routing root requests to a controller in a specific area, you can redirect the Browser to the URL of the area you want them to start on. So they start at "yoursite.com/" and will be redirected to the "yoursite.com/news/articles" URL. To do this, you create a root level controller and use the default route. So, create HomeController in the root controllers folder (not in an area). On that controller create an action method called Index. And in that Index method, redirect them to the controller you really want them to start on.

In global.asax, you can rely on a pretty standard default route like this:

routes.MapRoute(    
     "Default",  
     "{controller}/{action}/{id}",  
     new { controller = "Home", action = "Index", id = UrlParameters.Optional }  
); 

In your Index action on the Home controller, just redirect the client to the correct controller in the news area using RedirectToAction and specifying the area:

return RedirectToAction("IndexOfArticles", "Articles", new { area = "News" });

The second technique is to prioritize the route so it knows which area to use. This doesn't require any root level controllers. Just add a route entry that looks a little like this:

routes.MapRoute(
    "Default",
    "{controller}/{acion}/{id}",
    new {controller = "Articles", action = "IndexOfArticles", id = UrlParameters.Optional},
    new[] {"YourApplication.News"}
);

The drawback to this is that the 4th parameter (namespaces) is going to apply to all requests, so it may cause more to be redirected to your specific news area than you wanted. It might be better to be more specific about the second parameter so this route doesn't catch other requests... perhaps set the second parameter to an empty string so it only catches a site root request.

Stephen M. Redd
@Stephen: I do have Steven Sanderson's book. This is how I've been able to learn ASP.NET MVC alone (with stackoverflow as teacher, to be honest). Unfortunately, Routing has been so far (and by far) the hardest matter to understand. Things have started to change since yesterday when I got answers to my yesterday's question. I've better understanding.
Richard77
@Stephen: i just updated my answer but yours is more explanatory. I'll be reading Steven Sanderson's book soon to get a more understanding.
Manaf Abu.Rous
@Stephen: For sake of understanding, I'll consider the 2nd technique as well. But, Honestly, I'll stick with the 1st technique. In my opinion, it's cleaner and natural to do a redirection from the default action method. It's what I do anyway while writing code in a different controller/action. So I get to where I want when I run F5. Thanks very much.
Richard77
@Richard77: i want to point that Phill Haack have a nice mvc route debugger that you can use to examine your routes. you can find it here > http://haacked.com/archive/2008/03/13/url-routing-debugger.aspx
Manaf Abu.Rous
@Thanks to all. Get ready for the 2nd (or first, depending...) hardest matter: "Test Driven Development." on the upcoming questions.
Richard77