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).
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.
I have one area called user.
The area configuration looks like this
context.MapRouteLowercase(name: "User_Member", url: "User/Member", defaults: new { controller = "User", action = "Member", id = UrlParameter.Optional });
When I browse to this page without passing in a id, it returns the view my url looks like this /user/member
When I type the following into the browser /user/member/1
I've put a break point on
var userId
and it gets hit and I check the id parameter and its 1 which is correct.
[HttpGet]
[Authorize]
public ActionResult Member(Int64 id = 0)
{
var userId = id != 0 ? id : ReturnUserId();
var model = _userProfileBusinessLayer.GetProfile(userId);
return View(model);
}
Yet when I press F5 I get the following page
Server Error in '/' Application.
The view 'member' or its master was not found or no view engine supports the searched locations. The following locations were searched:
~/Views/user/member.aspx
~/Views/user/member.ascx
~/Views/Shared/member.aspx
~/Views/Shared/member.ascx
~/Views/user/member.cshtml
~/Views/user/member.vbhtml
~/Views/Shared/member.cshtml
~/Views/Shared/member.vbhtml
I'm unsure why I'm seeing that because all I have done is added /1 to the url?
I should be seeing the profile of the user which matches then id of 1, yet I remove /1 and it returns the view ?!?!?!?! slightly baffled
In your project, the area user should have a class that inherits from AreaRegistration e.g.
public class UserAreaRegistration : AreaRegistration
{
public override string AreaName
{
get
{
return "user";
}
}
}
In that class you can define routes sepcific for that area:
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"UserManagement",
"user/Admin",
new { controller = "UserAdmin", action = "Index" });
}
The file Global.asax.cs should have an Application_Start() method which calls:
AreaRegistration.RegisterAllAreas();
That will pick up the area routes you have configured. This is usually how area routes are configured. Do you have a similar setup for your project?
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'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.