ASP.NET MVC4 application with nested areas - c#

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.

Related

How to handle links with MVC 'Area' sections

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 = ""})

Areas Routing in MVC 4 with angularjs

Let me define my setup here. I have some areas like 1. AreaA 2. AreaB 3. AreaC and 4. AreaGeneral. in AreaGeneral I have one controller as home and GetData(paras) and it returns some data either in partial or full page form to angularjs. If I am using these two controller inside the AreaGeneral then its ok. but I want to provide the same controllers inside other Areas like in A, B, and AreaC, but there area and route are different. and more importantly when a user navigates to particular area say AreaB to access GetData(paras) controller then the url should be like .../AreaB/GetData instead of .../AreaGeneral/GetData and the route should work in other areas as well. Can someone guide me how to do this, Thanks.
Givent that you've already had AreaGeneral routes defined as below:
public class AreaGeneralAreaRegistration : AreaRegistration
{
public override string AreaName
{
get
{
return "AreaGeneral";
}
}
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"AreaGeneral_default",
"AreaGeneral/{controller}/{action}/{id}",
new { controller = "Home", action = "GetData", id = UrlParameter.Optional }
);
}
}
You can register that route in AreaB:
public class AreaBAreaRegistration : AreaRegistration
{
public override string AreaName
{
get { return "AreaB"; }
}
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"AreaB_Getdata",
"AreaB/Home/Getdata",
new { controller = "Home", action = "GetData" },
new string[] { "areastest.Areas.AreaGeneral.Controllers" }).DataTokens["area"] = "AreaGeneral";
context.MapRoute(
"AreaB_default",
"AreaB/{controller}/{actio‌​n}/{id}",
new { action = "Index", id = UrlParameter.Optional });
}
}
so that user can navigate to .../AreaB/Home/GetData and view the page actually handled within AreaGeneral.
I used angular services in different MVC areas for that I used following way
if your project structure should be like following
>ProjectName
>Areas
>AreaGeneral >controllername >GetData(function)
>AreaA
>AreaB
>AreaC
if your services like
app.service("yourService", ["$http",function ($http) {
this.GetData= function () {
var c = $http({
method: "post",
url:"../AreaGeneral/controllername/GetData",
data: "{}",
dataType: "json"
});
return c
} }])
if you are using inside AreaGeneral then
url "../AreaGeneral/controllername/GetData" ok for that
but when use different area like AreaA
"../AreaGeneral/controllername/GetData" its search inside the AreaA so that you donot get result
Solution work for me like called function adding extra ../ as per root directory structure
this way
"../../../AreaGeneral/controllername/GetData"
then every time call getdata function it checks from root directory so this way you can use Getdata function in any area
Hope this help you.

MVC5 Area not behaving properly

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.

C# MVC 4, Area Routes, using main views?

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).

MVC 4 admin area within separate project

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.

Categories

Resources