controller with the same name (home) in different areas

Asked

Viewed 147 times

-2

I have problem of ambiguity, when having controllers, with the same name as, Homecontroller in two areas: Admin and Backoffice is me presenting the problem of ambiguity, I’ve searched enough subject on the NET but nothing talks about it, besides being using Asp.net Core and MVC5

inserir a descrição da imagem aqui

switch (token.Perfil)
{
    case 1:
        redirection = RedirectToAction("Index", "Home", new { area = "Paciente" });
        break;
    case 2:
        redirection = RedirectToAction("Index", "Home", new { area = "Medico" });
        break;
    case 3:
        redirection = RedirectToAction("Index", "Home", new { area = "Farmacia" });
        break;
    case 4:
        redirection = RedirectToAction("Index", "Home", new { area = "Laboratorio" });
        break;

    default:
        redirection = RedirectToAction("Index", "Home", new { area = "Medico" });
        break;
}

My routes:

app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller=Login}/{action=Index}/{id?}");
            });

            app.UseMvc(routes =>
            {
                routes.MapRoute(
                  name: "areas",
                  template: "{area:exists}/{controller=Admin}/{action=Index}/{id?}"
               );

                //routes.MapRoute("areaRoute", "{area:exists}/{controller=Admin}/{action=Index}/{id?}");
                //routes.MapRoute(name: "default", template: "{controller=Home}/{action=Index}/{id?}");

            });
  • 1

    What problem is occurring? post and more details....

  • You must be in conflict in the route mapping table. Post the content of the method RegisterRoutes of HttpApplication.

  • I am using Asp.net core does not have Registrarroutes and Httpapplication

  • he owns Routes at startup along with Addmvc... but he doesn’t have the namespace to specify where he should look for homeController

1 answer

3


To avoid ambiguity, you must tick the area in the controller using the annotation "Area":

[Area("Paciente")]
public class HomeController : Controller
{
    // GET: /<controller>/
    public IActionResult Index()
    {
        return View("~/Areas/Paciente/Views/Paciente/Index.cshtml");
    }
}


[Area("Medico")]
public class HomeController : Controller
{
    // GET: /<controller>/
    public IActionResult Index()
    {
        return View("~/Areas/Medico/Views/Medico/Index.cshtml");
    }
}

You can test my sample application by clicking Login -> Redirect2

https://github.com/superrfm/aspnetcore_areas

  • That’s right, unfortunately I think this is very wrong, because if you are already within an area you still have to mention that the controller is within the area? In addition to being ultra redundant, thank you for helping Rafael

  • You find this because the folder structure and its routes are identical, but this doesn’t have to be so, so you haven’t actually defined the area anywhere. Take the example: "{area:exists}/{controller=Home}/{action=Index}/{id? }" - action matches by method name, controller - match by controller name, and then area matches by attribute. If you want you can create basic controllers in the folders and just put the attribute and the other controllers inherit from it.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.