Asp.Net MVC - Routing issue with same name controllers? - asp.net-mvc

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 }
);

Related

Handling Bad URLs in ASP.NET MVC

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.

How to use Web API with Areas and Same controller name in different area?

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 }
);

MVC Areas - Controller does not return view

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 .

How to configurate a View of a particular Area as the main entry of an MVC4 Application?

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

Mvc area routing?

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

Resources