ASPNET MVC: ow can I make a page located in a given area (as opposed to the root structure) as default?
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.Option开发者_StackOverflowal } // 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.
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.
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.
精彩评论