I'm building an ASP.NET MVC 4 application and would like to have my site admin area as an MVC Area within a separate project.
I've added a new MVC 4 Web Application project to my solution and added the following file to register my area:
public class AdminAreaRegistration : AreaRegistration
{
public override string AreaName
{
get { return "Admin"; }
}
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Admin_default",
"Admin/{controller}/{action}/{id}",
new { controller = "Home", action = "Index", area = "Admin", id = UrlParameter.Optional },
new[] { "Future.Admin.Controllers" }
);
}
}
However this results in the following error:
A route named 'Admin_default' is already in the route collection. Route names must be unique.
I've altered the build output path of my admin area project to point to the main site project.
Is there something I'm missing or not configured?
Any help is much appreciated!
If this route is only declared once in your codebase, clean and rebuild your solution. This can happen if you have "old" dlls (for example if you had renamed a project) in your bin folder, with this route registered.
Related
I have developed a project in C# MVC with 'AdminLte' Template and I uses Areas for submodules.
when I access a link from home page (http://localhost:9760/Home/Index) the it works perfectly as follows http://localhost:9760/Manage/ChangePassword
but when I access the same link from Area ex: 'AirSurveillance' http://localhost:9760/AirSurveillance/Manage/ChangePassword it is not working and gives 404 error because it tried to find 'Manage' controller (which is not there) in 'AirSurveillance' area.
this is my auto-generated 'SecurityClearanceAreaRegistration.cs' file
namespace IIMS.Areas.SecurityClearance
{
public class SecurityClearanceAreaRegistration : AreaRegistration
{
public override string AreaName
{
get
{
return "SecurityClearance";
}
}
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"SecurityClearance_default",
"SecurityClearance/{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional }
);
}
}
}
Please help me.
When using areas, your links will need to specify the area for the controller/action you're pointing to. If you want to use controllers outside of those areas you specify a blank Area. for example...
#Html.ActionLink("ChangePassword", "Manage", new { Area = ""})
This post is directly related to: MVC5 Area not working
That post fixed the index.cshtml issue, however it did not resolve each view for that controller. For example:
http://localhost:45970/Setup/Start gives the error that the resource cannot be found (basically a 404).
However http://localhost:45970/Setup/Setup/Start brings up the correct page.
So what needs to be reconfigured so that ALL views for that controller in the Setup Area will open properly?
Edit 1
Code from SetupAreaRegistration.cs
using System.Web.Mvc;
namespace BlocqueStore_Web.Areas.Setup
{
public class SetupAreaRegistration : AreaRegistration
{
public override string AreaName
{
get
{
return "Setup";
}
}
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
name: "Setup_default",
url: "Setup/{controller}/{action}/{id}",
defaults: new { action = "Index", controller = "Setup", id = UrlParameter.Optional },
namespaces: new[] { "BlocqueStore_Web.Areas.Setup.Controllers" }
);
}
}
}
Since you have an Area named Setup with the above route configuration, opening http://localhost:45970/Setup/Start will execute StartController under Setup Area. You got 404 error because you don't have StartController under Setup Area, but you can open http://localhost:45970/Setup/Setup/Start successfully because you have SetupController and Start action method under Setup Area.
Based on your comment, you want the following url patterns
http://{host}/Setup/{view}
http://{host}/Admin/{view}
You can accomplish that without using any Area. You only need AdminController and SetupController using the default route.
My MVC application is set up with controllers at the root/global level. These have no explicit Area. I also have a single "Admin" area. URLs that begin with /admin/ are routed to the matching controller in the Admin area. Other URLs are routed to the matching global controller. This is working fine for the most part.
The issue I'm having is that in the case when a URL matches a controller in the Admin area when one of the same name doesn't exist on the global area, the request is incorrectly routed to the controller in Admin area. I know this is happening because I put a breakpoint on the matching action in the relevant controller.
For example, I have a controller called CalendarController in the Admin area. When I visit /admin/calendar, it works because it finds the action and the corresponding view in Areas/Admin/Views/Calendar/Index.cshtml. The problem occurs when I visit /calendar. I do not have a controller named Calendar at the root level, but for some reason it routes the request to the Admin area's CalendarController, which I don't want. I want it to return a 404 because no CalendarController exists at the root level. Instead, I get an error because it's searching for the view at the root level (at /Views/Calendar/Index.cshtml) even though the matching controller was in the Admin area.
How can I prevent the Admin area from being searched for matching controllers except when the URL has /admin in it?
Here's the relevant route code, which is basically stock except for the namespaces addition. The issue still happens without the namespace. There are more routes in the actual application, but I'm getting the same behavior in a brand new MVC project.
public class AdminAreaRegistration : AreaRegistration
{
public override string AreaName
{
get
{
return "Admin";
}
}
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Admin_default",
"Admin/{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional },
new[] { "AreaProblem.Areas.Admin.Controllers" }
);
}
}
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
What I mean by a controller at the "root level" is Controllers/HomeController in this screenshot. I want URLs that don't start with /admin to only look at those controllers. The problem is that it's also searching in Areas/Admin/Controllers.
So the MVC routing engine will look in different namespaces to try and find a matching controller. You can solve this by specifying a namespace (like you did for the admin area). You can also specify that a route not search other namespaces using DataTokens.
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional },
namespaces: new [] { "AreaProblem.Controllers" }
).DataTokens["UseNamespaceFallback"] = false;
Previously I built an pluggable mvc4 app with a concept of areas in MVC4 as described in this article. I was facing problem in deploying this app and I solved it as described here.
Now I want to go one step further. I want to use nested areas i.e. area within area.
Following is my project structure:
Here MainProj is the main project whereas other projects are areas.
Now, in CRM area I want to add Area "ManageAppointments" (this is the nested area)
I am able to add this sub area but facing problem in routing. View engine never finds views present within ManageAppointments area.
I supposed this problem is because, the routing is aware of the areas within the main project, but it is not aware of that there is area within area. In simpler way, routing is aware of CRM area but it never searches for MangeAppointment area within CRM area i.e. it is not aware that there is again another area within CRM area.
Currently my area registration for CRM and MangeAppointment is as follows:
CRM:
public class CRMAreaRegistration : AreaRegistration
{
public override string AreaName
{
get
{
return "CRM";
}
}
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"CRM_default",
"CRM/{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional },
new string[] { "CRM.Controllers" }
);
}
}
MangeAppointment:
public class MangeAppointmentAreaRegistration : AreaRegistration
{
public override string AreaName
{
get
{
return "MangeAppointment";
}
}
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"MangeAppointment_default",
"MangeAppointment/{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional },
new string[] { "MangeAppointment.Controllers" }
);
}
}
I think there is something wrong with MangeAppointment Area Registration.
Thanks in advance.
simply change your MangeAppointment area registration as follows:
public override string AreaName
{
get
{
return "CRM/Areas/MangeAppointment";
}
}
If you simply return "MangeAppointment" it will search for ManageAppointment within main project i.e. it will search in location MainProj/Areas/MangeAppointment which is wrong location.
But when you return CRM/Areas/MangeAppointment it will search in the
location MainProj/Areas/CRM/Areas/MangeAppointment.
I have a basic project with areas in it, I have registered my routes in this area and have used a Lowercase URL extension. Here is the code:
using System.Web.Mvc;
using Web.Modules;
namespace Web.Areas.Services
{
public class ServicesAreaRegistration : AreaRegistration
{
public override string AreaName
{
get
{
return "Services";
}
}
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRouteLowercase(
"Services", // Route name
"services/{controller}/{action}/{id}", // URL with parameters
new { controller = "Services", action = "Index", id = UrlParameter.Optional }, // Parameter defaults
new string[] { "Web.Areas.Services" }
);
}
}
}
But now when I go to http://localhost/services/home it shows me my HomeController's Index View, what can I do to fix this? I have already added the namespaces and added the area to the routedata.
Thanks for any assistance
Seems like you either don't have the folder Areas/Services/Views/Home at all or the Index view in that folder. In this case, ASP.NET MVC will fallback to displaying your Index view from the Views/Home (no area) folder. If you need a different view for your area, you need to create it in the area's Views/[Controller Name] folder (or the Shared folder, of course).