36

Is anyone knows how to Configure Areas in ASP.NET MVC3. I read an article about Areas in here. But that article is not based on MVC3. In MVC3 there is no function named MapRootArea in RouteCollection routes which is found in Global.asax

routes.MapRootArea("{controller}/{action}/{id}", "AreasDemo", new { controller = "Home", action = "Index", id = "" }); 

When i create a New Area using MVC3, i got a class of that area which inherited from AreaRegistration and look like following: (here Blogs is the area name)

public class BlogsAreaRegistration : AreaRegistration { public override string AreaName { get { return "Blogs"; } } public override void RegisterArea(AreaRegistrationContext context) { context.MapRoute( "Blogs_default", "Blogs/{controller}/{action}/{id}", new { action = "Index", id = UrlParameter.Optional } ); } } 

Would anyone please help me how do i configure area in MVC3. Any kind of link would be helpful also.

3 Answers 3

40

Right click on your web project and select Add -> Area... Then type the name of the area and Visual Studio will take care of the rest which is to generate all the necessary classes. For example the area registration might look like this:

public class AreasDemoAreaRegistration : AreaRegistration { public override string AreaName { get { return "AreasDemo"; } } public override void RegisterArea(AreaRegistrationContext context) { context.MapRoute( "AreasDemo_default", "AreasDemo/{controller}/{action}/{id}", new { action = "Index", id = UrlParameter.Optional } ); } } 

and in Application_Start of your Global.asax all you need is:

AreaRegistration.RegisterAllAreas(); 
Sign up to request clarification or add additional context in comments.

9 Comments

thanks for the answer.How can i know that the Area is Registered properly?. I used this link in my shared _layout but it's not taking me anywhere @Html.ActionLink("Blog", "About", "Home", new { area = "Blog"})
@Imrul, from what I can see in your code your area is called Blogs, not Blog, so try: @Html.ActionLink("Blog", "About", "Home", new { area = "Blogs" }). Also make sure that there is a HomeController inside this area.
sorry, for the spelling mistake. I think i got my problem, if i render this to my _layout it doesn't generate a link to Area/Controller/Action "@Html.ActionLink("Blog", "Index", "BlogHome", new { area = "Blogs"})". Am i doing anything wrong in ActionLink helper? and i discover now that same controller name can't be possible in root and Area. For that i had to rename Home to BlogHome. and FYI <a href="/?Length=8" area="Blogs">Blog</a> is the Generated html and using localhost:4135/Blogs/BlogHome/Index hits the BlogHome controller and shows the page.
Your using wrong overload, try this one: @Html.ActionLink("Blog", "Index", "BlogHome", new { area = "Blogs" }, null)
thanks, @frennky. It works like a charm. Would you please give me any link, where can i find these changes. I Googled it but there is no help. Where can i find all the functions/helpers of ASP.NET MVC3?
|
5

You can have the same controller name in the root and the area, you just have to define it.

In your global.asax, add the last line of the routes.maproute as shown below

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

also, add the name of the controller in your ares/?????AreaRegistration.cs file

 context.MapRoute( "Membership_default", "Membership/{controller}/{action}/{id}", new { controller= "Home", action = "Index", id = UrlParameter.Optional } ); 

Comments

1

please find below image shows how to configure area in mvc .enter image description here

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.