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.
Related
The following Error i faced :
Multiple types were found that match the controller
named 'test'. This can happen if the route that services this request
('JIB/api/{controller}/{action}') found multiple controllers defined
with the same name but differing namespaces, which is not supported.
The request for 'test' has found the following matching controllers:
WebApplication2.Areas.JIB.Controllers.TestController
WebApplication2.Areas.JCB.Controllers.TestController
System.InvalidOperationException
at
System.Web.Http.Dispatcher.DefaultHttpControllerSelector.SelectController(HttpRequestMessage
request) at
System.Web.Http.Dispatcher.HttpControllerDispatcher.d__1.MoveNext()
[Route("JIB/api/Test/test")]
[HttpGet]
public IHttpActionResult Test()
{
return Ok("JIBs");
}
--------------
[Route("JCB/api/Test/test")]
[HttpGet]
public IHttpActionResult Test()
{
return Ok("JCB");
}
---------------
Global.asax
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapMvcAttributeRoutes();
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
}
protected void Application_Start()
{
JCBAreaRegistration.RegisterAllAreas();
// GlobalConfiguration.Configure(WebApiConfig.Register);
RegisterRoutes(RouteTable.Routes);
}
JCBAreaReges
context.Routes.MapHttpRoute(
name: "JCBApiAction",
routeTemplate: "JCB/api/{controller}/{action}"
);
context.Routes.MapHttpRoute(
name: "JCBApi",
routeTemplate: "JCB/api/{controller}"
);
//****************=======Default Route=========*******************
context.MapRoute(
"JCB_dashboard",
"JCB/{controller}/{action}/{id}",
new { Controller = "Dashboard", action = "Index", id = UrlParameter.Optional }
);
context.MapRoute(
"JCB_default",
"JCB/{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional }
);
JIBAreaReges
context.Routes.MapHttpRoute(
name: "JIBApiAction",
routeTemplate: "JIB/api/{controller}/{action}"
);
context.Routes.MapHttpRoute(
name: "JIBApi",
routeTemplate: "JIB/api/{controller}"
);
//****************=======Default Route=========*******************
context.MapRoute(
"JIB_dashboard",
"JIB/{controller}/{action}/{id}",
new { Controller = "Dashboard", action = "Index", id = UrlParameter.Optional }
);
context.MapRoute(
"JIB_default",
"JIB/{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional }
);
The structure of webapi is as follows:
App
-Area
-MyArea1
- ControllerA (Route('api/myarea1/controllera'))
-MyArea2
- ControllerA (Route('api/myarea2/controllera'))
Problem is that the route api/myarea1/controllera and api/myarea2/controllera are not being resolved. It comes are 404.
I read somewhere we need to implement IHttpControllerSelector but not sure what is the simplest way to implement this. If there is any other way it can be done?
Any idea.
Edit:
RouteConfig.cs
public class 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 }
);
}
}
WebApiConfig.cs
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
}
Edit 2:
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapHttpRoute(
name : "MyArea1_default",
routeTemplate : "MyArea1{controller}/{action}/{id}",
defaults : new { action = "Index", id = UrlParameter.Optional }
);
}
If you want to use controllers with the same name, you need to specify the namespace for each route configuration.
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapHttpRoute(
name : "MyArea1_default",
routeTemplate : "MyArea1{controller}/{action}/{id}",
defaults : new { action = "Index", id = UrlParameter.Optional },
namespaces: new[] { "Your.Controller.Namespace.Here" }
);
}
For your reference:
http://haacked.com/archive/2010/01/12/ambiguous-controller-names.aspx/
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 }
);
please I need help with this scenario:
I have an Area "\UI" with one Controller "ActionController" and his View "Login.cshtml" also in his UIAreaRegistration class:
public class UIAreaRegistration : AreaRegistration
{
public override string AreaName
{
get { return "UI"; }
}
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"UI_default",
"UI/{controller}/{action}/{id}",
new { controller = "Account", action = "Login", id = UrlParameter.Optional }
);
}
}
Now I want Login.cshtml to be the first view of the app.
Then in 'RegisterRoutes' of global.asax.cs I have:
AreaRegistration.RegisterAllAreas();
//Default
//routes.MapRoute(
// name: "Default",
// url: "{controller}/{action}/{id}",
// namespaces: new[] { "", "" },
// defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
//);
routes.MapRoute(
name: "Default",
url: "{area}/{controller}/{action}/{id}",
defaults: new { area = "UI", controller = "Account", action = "Login", id = UrlParameter.Optional }
);
But didn't work, please what should I do?
Thanks in advance
What you are trying to do is to make your UI area a default area, so any controller in UI will be accessible without UI prefix in URL.
The problem with that is that the controllers which are in root folder will no longer be accessible. If it is what you want you can do it by changing the area route registration from :
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"UI_default",
"UI/{controller}/{action}/{id}",
new { controller = "Account", action = "Login", id = UrlParameter.Optional }
);
}
to:
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"UI_default",
"{controller}/{action}/{id}",
new { controller = "Account", action = "Login", id = UrlParameter.Optional }
);
}
Easier solution will be to create an action on the default controller which will redirect user to your area
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