How to use areas only using the root controller

1

I created an ASP.NET MVC 5 project where I had made a template , so now we will have several templates divided by areas . But the controllers will be the same, so I did not want to create the same controllers for all areas . How do I do this?

 //exemplo link html 
     <a href='/home/index'> Home </a>

//controller:
    public ActionResult Index(){
        //aqui faço a verificação que retornará a area

        string retorno = obterAreaUtilizada(); //ex.:"template02"

        return RedirectToAction("Index","Home", new { area = retorno });
    }

But I'm testing here and it's giving me error, for not just using controller in the areas ... could anyone help me?

EDIT

I found a way, but I need to know if it's consistent, not a gambiarra:

    public ActionResult Index(){
        //aqui faço a verificação que retornará a area

        string retorno = obterAreaUtilizada(); //ex.:"template02"

        string caminho = string.format("~/areas/{0}/views/home/index.cshtml",retorno);
        return View(caminho);
    }

Please, if anyone knows a better solution, it would be great if you shared it. Many thanks.

    
asked by anonymous 27.08.2016 / 04:08

1 answer

1
  

I found a way, but I need to know if it's consistent, not a gambiarra.

It's a gambiarra. You are actually having the Controller have to interpret the route by getting the requested area per request, which is wrong. The role of Controller is not to interpret routes, but to process a request made for a particular route.

The correct way to do this is to register your Controller in a namespace common to all routes. For example:

namespace MeuProjeto.Controllers.AreasComum
{
    public class MeuControllerDeArea : Controller { ... }
}

When creating an area, a file named NomeDaAreaAreaRegistration.cs is created that registers a route to that area. Just complete the route parameterization of this file as follows:

namespace MeuProjeto.Areas.MinhaArea
{
    public class MinhaAreaAreaRegistration : AreaRegistration
    {
        public override string AreaName
        {
            get
            {
                return "MinhaArea";
            }
        }

        public override void RegisterArea(AreaRegistrationContext context)
        {
            context.MapRoute(
                name: "MinhaArea_default",
                url: "MinhaArea/{controller}/{action}/{id}",
                defaults: new { controller = "MeuControllerDeArea", action = "Index", id = UrlParameter.Optional },
                namespaces: new string[] { "MeuProjeto.Controllers.AreasComum" }
            );
        }
    }
}

Typically, write your Controllers only by creating their Views files in the directories expected of each Controller within the Views directory of each one of your Areas .

Create as many Areas as necessary by repeating the route registration process for each Area .

Warning: If there are two Controllers with the same name in different namespaces , route control can be lost. If this occurs, you need to explain to the ASP.NET MVC what the default namespace for Controllers looks like this:

public class MvcApplication : System.Web.HttpApplication
{
    protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);
        // Insira a linha abaixo com o namespace padrão dos seu Controllers que não pertencem a Areas
        ControllerBuilder.Current.DefaultNamespaces.Add("MeuProjeto.Controllers");
    }
    ...
}
    
27.08.2016 / 07:08