1
I created an ASP.NET MVC 5 project where I had done one template, so now we’re going to have several templates divided by areas. Only that the controllers will be the same, so I didn’t want to create the same controllers for all the areas. How do I do it?
//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 });
}
Only I’m testing here and it’s making a mistake, by just not using controller in the areas... someone could help me?
EDIT
I’ve found a way, but I need to know if she’s coherent, not a gambit:
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 more suitable solution, it would be great if they shared it. Thank you very much.
Thank you very much for the reply Gypsy, I have a doubt, how would the url look like this way? why the idea is to do type www.algumacoisa.com/home/index and through the controller I redirect to my view. if I do what you said by entering this url as it would be the return of the controller?
– Vinicius.Beloni
or would use the first code block with redirectToView?
– Vinicius.Beloni
www.algumacoisa.com/home/index
does not enter any area. Entering area would be something likewww.algumacoisa.com/area/home/index
. The return is almost the same as the Controllers but in an isolated context Controllers normal.– Leonel Sanches da Silva
So the way you explained I would have to use the .../myArea/... to access it?
– Vinicius.Beloni
Exactly. That’s what the areas are for.
– Leonel Sanches da Silva
redirectToAction would be a good option to make use of areas without making it explicit in the url? as I said is a visual change where areas are just views and js of them, some even as shortcut for being the same views.
– Vinicius.Beloni
Areas are not resources for changing templates.
RedirectToAction
is a function used both by Controllers of Areas as Controllers root. You are trying to use Areas in a wrong way. I suggest asking another question being more specific than what you want is a change of templates in accordance with specific criteria.– Leonel Sanches da Silva
Oh yes, thank you very much Gypsy. I will mark your explanation as an answer because I understood better about it. Thanks !
– Vinicius.Beloni
@cigano_morrison_mendez cigano, I asked another more specific question, http://answall.com/questions/149864/como-trocar-o-templatevisual-de-uma-pagina-asp-net-mvc5-de-acordo-usu%C3%A1rio
– Vinicius.Beloni