Default view for an area - asp.net-mvc

I am using ASP.NET MVC 3. I created an area called Administration. There is no default view associated with it, so if I type in www.mywebsite.com/Administration then there is an error. How would I go and define a default view when the user types in the above mentioned URL? Would I need to go and create a Home controller?
I would like to have something like:
www.mywebsite.com/Administration or
www.mywebsite.com/Administration/Index
AdministrationAreaRegistration.cs has the following:
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Administration_default",
"Administration/{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional }
);
}
Not sure if this is possible?

You are about not to define default view, but default action. The code above misses the type of controller, action of which you want to use as default:
Suppose you have AdministrationHome controller.
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Administration_default",
"Administration/{controller}/{action}/{id}",
new { action = "Index", controller="AdministrationHome", id = UrlParameter.Optional }
);
}

Perhaps make a Controller/Views called Administration in the root (not in the Administration area).
Call the view index.

Related

using Areas in MVC5

I am trying to achieve the routings as follow:
http://example.com/Admin/Index
http://example.com/Application/Index
http://example.com/Customers/Index
etc...
I like the ideas of using 'Areas' and want to separate all the codes by using Areas. So, I created my Areas structure like the following screnshot
and the code in ApplicationAreaRegistration.cs is
public override string AreaName
{
get
{
return "Application";
}
}
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Application_default",
"Application/{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional }
);
}
However, I cannot achieve the route I want like
http://example.com/Application/Index
In stead of that it becomes, http://example.com/Application/Application/Index
I tried to change the default routing without {controller} in AreaRegistration
context.MapRoute(
"Application_default",
"Application/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional }
);
But I got, Controller is required area.
I know I can easily get http://example.com/Application/Index if I put the Controller in root Controllers Folder. But it means I couldn't group my codes like the Areas anymore and it will be seprated across the MVC Folders.
What I would like to know is, whether I can achieve what I want by using the Areas or am I trying to do which is impossible?
You need to add a default controller name to the route so MVC understands what to put in the controller route value when you take it out of the URL.
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Application_default",
"Application/{action}/{id}",
new { controller = "Application", action = "Index", id = UrlParameter.Optional }
);
}

Area routing, why is the area name needed?

I have a project with 2 areas. Its does work but I am a newbie to this and I want to understand why.
I have an Area called LogonArea
context.MapRoute(
"LogonArea_default",
"LogonArea/{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional }
Why is the 'LogonArea/' part needed? Shouldn't it be able to find the controller without it?
When I tried removing it I could still reach controllers with that Area but strangely I couldn't reach other areas while on that page.
If this is really necessary how could I mask it so the Area wasn't visible in the url?
thanks
If you remove /LoginArea/ from the area route registration, it will be able to find your controller (as long as you don't have any conflicting controller names such as HomeController in the main section and HomeController in the area).
It's mainly there for your convenience. If you have an Admin area, everything in your site will be accessible via /Admin/{controller}. It's mostly just an organizational thing.
public class AdminAreaRegistration : AreaRegistration
{
public override string AreaName
{
get
{
return "Admin";
}
}
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Admin_default",
"{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional }
);
}
}
I created a single controller called FooController in this project, and I was able to go to the url /Foo to reach it without needing to go to /Admin/Foo
When you create a link to a controller outside of the area you need to specify which area it's in (or specify that there is no area):
#Html.ActionLink("Go Home", "Index", "Home", new { area = "" }, null)

Adding area to mvc project

I have an mvc project and I added a new area with the name BEK
and BEKAreaRegistration.cs was created.
public class BEKAreaRegistration : AreaRegistration
{
public override string AreaName
{
get
{
return "BEK";
}
}
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"BEK_default",
"BEK/{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional }
);
}
}
and my global.asax file is as follows:
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
RouteTable.Routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
RouteTable.Routes.IgnoreRoute("{*allAspx}", new { allAspx = #".*\.aspx(/.*)?" });
RouteTable.Routes.IgnoreRoute("{*allAsmx}", new { allAsmx = #".*\.asmx(/.*)?" });
RouteTable.Routes.IgnoreRoute("{*allAshx}", new { allAshx = #".*\.ashx(/.*)?" });
RouteTable.Routes.IgnoreRoute("Services/{*pathInfo}");
RouteTable.Routes.IgnoreRoute("");
RouteTable.Routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
but when i try to go to BEK/Home/Index page I get an error page. What else should I do?
This is the error:
The resource cannot be found. Description: HTTP 404. The resource
you are looking for (or one of its dependencies) could have been
removed, had its name changed, or is temporarily unavailable. Please
review the following URL and make sure that it is spelled correctly.
Requested URL: /LMS_WEB_APP/BEK/Home
-------------------------------------------------------------------------------- Version Information: Microsoft .NET Framework Version:4.0.30319;
ASP.NET Version:4.0.30319.18213
When you add BEK Area.
MVC will create These for you.
Mvc will not create any Controller and Actions and Views.
So, you do have to do that manually that which controllers, actions and views you want to add.
So, now you do have to Add the Controller by Right Clicking on Controller and Add Controller.
After Adding the Controller.
You can do right click on the Action and Add View like this to add the View :
Ok....
So, you do have Both Controller and Action and Views required.
Now you might have to Resolve the Controller Duplicacy, if any, which i had told you earlier.
Happy Coading...
I think problem is that, You have Same Name controller in Both Area and and application. Like You have Home Controller in Normal Application and Also in AREA
And, it is causing the Duplicate Declaration of Same Controller.
The way doing that is, specifying the NAMESPACE of the Controller Like the Following :
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"BEK_default",
"BEK/{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional },
new string[] { "MyAppName.Areas.BEK.Controllers" } // specify the new namespace
);
}
If not that Case, Please post the Error Message you are getting.

MVC4 routing 2 areas containing controllers with the same name

I have 2 areas in an mvc4 application and I have registered the namespace for each of the areas.
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Intergration_default",
"Intergration/{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional },
constraints: null,
namespaces: new[] { "WebApplication.Areas.Intergration.Controllers" }
);
}
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Vend_default",
"Vend/{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional},
constraints: null,
namespaces: new[] { "WebApplication.Areas.MyController.Controllers" }
);
I can access Intergration/MyController however when I try accessing MyController I get an error
Multiple types were found that match the controller named 'mycontroller'. This can happen if the route that services this request ('{controller}/{action}/{id}') does not specify namespaces to search for a controller that matches the request. If this is the case, register this route by calling an overload of the 'MapRoute' method that takes a 'namespaces' parameter.
What am I doing wrong? do I need to do something extra in the global.asax
Based on the code / description you provided, it sounds like it could be a couple of things:
You have a controller name collision in the root controllers namespace (i.e., in the Controllers folder in the root of the project, not in an area) with another area with no constraint.
More likely, your second area registration for Vend has what looks like an incorrect namespace. Instead of WebApplication.Areas.MyController.Controllers it should be WebApplication.Areas.Vend.Controllers. I bet that there's a controller in your root controllers namespace that shares a controller name with something in the Vend area.

Trouble setting a default controller in MVC 2 RC Area

This should be simple, but alas...
I've set up an Admin area within my MVC 2 project (single project areas). I've created a couple controllers and their respective view folders. In the AreaRegistration.RegisterArea method, I've specified that I want the default controller to be "Dashboard":
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Admin_default",
"Admin/{controller}/{action}/{id}",
new { controller = "Dashboard", action = "Index", id = "" }, new string[] { "Admin" }
);
}
If I navigate to url/Admin/Dashboard, the index comes up just fine. What I want, though, is to allow the user to go to url/Admin/ and see the same thing. When I do this, however, I get "The resource cannot be found".
I'm just getting my feet wet with MVC 2's Area implementation, and I don't think I'm doing anything overly complicated... Anyone had the same problem? Do I need to specify a separate route, perhaps at the root, non-area level?
Try adding this additional route:
context.MapRoute(
"Admin_default2",
"Admin"
new { controller = "Dashboard", action = "Index", id = "" }
)
Ok, odd. So I added a different area, aptly named "Administration", set the default controller and added the appropriate controller, view, etc. and it worked. The difference? In my first case, I was using "Admin" as the area.
context.MapRoute(
"Admin_default3",
"Admin/{action}",
new { controller = "Admin", action = "Index" }
);

Resources