Area folders look like :
Areas
Admin
Controllers
UserController
BranchController
AdminHomeController
Project directories look like :
Controller
UserController
GetAllUsers
area route registration
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Admin_default",
"Admin/{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional },
new { controller = "Branch|AdminHome|User" }
);
}
project route registration
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
namespaces: new string[] { "MyApp.Areas.Admin.Controllers" });
}
When I route like this: http://mydomain.com/User/GetAllUsers I get resource not found error (404). I get this error after adding UserController to Area.
How can I fix this error?
Thanks...
You've messed up your controller namespaces.
Your main route definition should be:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
namespaces: new string[] { "MyApp.Controllers" }
);
And your Admin area route registration should be:
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Admin_default",
"Admin/{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional },
new { controller = "Branch|AdminHome|User" },
new[] { "MyApp.Areas.Admin.Controllers" }
);
}
Notice how the correct namespaces should be used.
An up to date solution for ASP.NET Core MVC.
[Area("Products")]
public class HomeController : Controller
Source: https://learn.microsoft.com/en-us/aspnet/core/mvc/controllers/areas
Related
I am working on ASP.NET MVC with Areas. I have three Areas. However, the default route is not an Area. When I ran the Application, I got this error:
The Routes are shown below:
RouteConfig
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
namespaces: new string[] { "SmartSIMS.Web.Controllers" }
);
}
The Areas are:
Administration
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
name: "Administration_default",
url: "Administration/{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
namespaces: new string[] { "SmartSIMS.Web.Areas.Administration.Controllers" });
}
Students
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
name: "Students_default",
url: "Students/{controller}/{action}/{id}",
defaults: new { action = "Index", id = UrlParameter.Optional },
namespaces: new string[] { "SmartSIMS.Web.Areas.Students.Controllers" });
}
Teachers
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
name: "Teachers_default",
url: "Teachers/{controller}/{action}/{id}",
defaults: new { action = "Index", id = UrlParameter.Optional },
namespaces: new string[] { "SmartSIMS.Web.Areas.Teachers.Controllers" });
}
How do I resolve this error? Kindly assist.
Problem resolved. I turned RouteDebugger in the web.config to false.
I have two different areas in ASP.NET MVC Web Application.
1. Admin
2. Site
I want to have the default route of my application to the Site Area
i.e www.mydomain.com/Site/Home
For that I have configured the default route as follows.
But this configuration does not work for me.
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
namespaces: new string[] { "MyApp.Areas.Site.Controllers" }
);
}
The Site Area Route is as follows.
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
name: "Site_default",
url: "Site/{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional}
);
}
The Admin Area Route is as follows.
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Admin_default",
"Admin/{controller}/{action}/{id}",
defaults: new { controller="Login",action = "Login", id = UrlParameter.Optional}
);
}
Your help will be much appreciated
I think you've wrong controller namespaces.
Your main route definition should be:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
namespaces: new string[] { "MyApp.Controllers" }
);
And your areas route like admin area registration should be:
context.MapRoute(
"Admin_default",
"Admin/{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional },
new { controller = "Home" },
new[] { "MyApp.Areas.Admin.Controllers" }
);
Following steps to do:
Create a Project: Project Type: Framework 4.6.1 > WebApi and MVC Web
Project Layout will have [Areas Folder]
Inside [Areas folder] you'll find folder [HelpPage Folder]
Right-click >> [Areas folder] Select Area > "Give it a name: Administration"
Visual Studio Register everything for you.
Areas
Administration
Controllers
Models
Views
AdministrationAreaRegistration.cs
Filestructure example
Create a Controller > read write : Give it a name [Admin] and [View] Indexenter image description here
Then in your layout add link : Under the normal Home link
#Html.ActionLink("Home", "Index", "Home", new { area = "" }, null)
#Html.ActionLink("Admin", "Index", "Admin", new { area = "Administration" }, null)
Click on the link and see your Administration area
I have some problems using Areas in my MVC project. I'm able to access my controller, which is located under the area, but when it returns to view (with model), I get an error:
The view 'Index' or its master was not found or no view engine
supports the searched locations.The following locations were searched:
~/Views/MyController/Index.aspx ~/Views/MyController/Index.ascx etc.
Here is the MyAreaAreaRegistration:
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"MyArea_default",
"MyArea/{controller}/{action}/{id}",
new { controller = "MyController", action = "Index",
id = UrlParameter.Optional });
}
Routeconfig:
routes.MapRoute(
name: "Default", // Route name
url: "{controller}/{action}/{id}", // URL with parameters
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
namespaces: new[] { "MyApp.Controllers" }
);
Global.asax:
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
...
RouteConfig.RegisterRoutes(RouteTable.Routes);
}
And controller:
return View(myViewModel);
I'm totally stuck with this one. Any help would be much appreciated.
Please change your route config code .
routes.MapRoute(
name: "Default", // Route name
url: "{controller}/{action}/{id}", // URL with parameters
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
).DataTokens.Add("area", "MyArea"); ;
It will be work for you .
Thanks .
I am using mvc4 framework and .net framwork 4.5. I need a url like this:
www.examples.com/name (note: 'name' will be change dynamically)
which routes to the same page.
I have tried like this but getting error
My action method is like this:
public ActionResult Userlist(string status)
{
return View();
}
Route Config
routes.MapRoute(
"user",
"{status}",
new { controller = "Home", action = "Userlist" }
);
How can I create a route syntax and redirect this to a controller?
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
// Add this route config
routes.MapRoute(
name: "Default_Userlist",
url: "{status}",
defaults: new { controller = "Home", action = "Userlist" },
namespaces: new[] { "MyMvcProject.Controllers" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
namespaces: new[] { "MyMvcProject.Controllers" }
);
}
Don't forget to replace the "MyMvcProject" with the name of your project.
Issue
This is a bad practice because the value of the status could easily cause conflicts with your action methods.
The problem I'm facing is that I have 2 controllers with the same name. One in the main controller folder, and the other in the controller folder in my Admin Area.
Calling the action result directly works fine:
MySite/Admin/Account/GetAccount?userId=1
Calling through the route doesn't work
MySite/Admin/User/1/Account
Any idea What I'm doing wrong?
Application_Start
AreaRegistration.RegisterAllAreas()
RouteConfig
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional }, // Parameter defaults
new[] { "MyCompany.Controllers" }
);
}
AdminAreaRegistration
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Admin_default",
"Admin/{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional }
);
context.MapRoute(
"GetUserAccount",
"Admin/User/{userId}/Account",
new { controller = "Account", action = "GetAccount" },
new[] { "MyCompany.Areas.Admin.Controllers" }
);
}
My Action Result In Areas/Admin/AccountController
public ActionResult GetAccount(string userId)
{
// return Account Type
}
i think you should change the positions of the account and check again
context.MapRoute(
"GetUserAccount",
"Admin/User/{userId}/Account",
new { controller = "Account", action = "GetAccount" },
new[] { "MyCompany.Areas.Admin.Controllers" }
);
context.MapRoute(
"Admin_default",
"Admin/{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional }
);