Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC - Can i have multiple names for the same action?

Tags:

asp.net-mvc

ASP.NET MVC - Can i have multiple names for the same action?

In the same controller... Can i have multiple names for the same action?

I am looking for a complete multiple language solution. Essentially the i want all the logic to be sa same but change the "keywords" (actions, controllers in the url) depending on language.

like image 578
unom Avatar asked Apr 26 '10 00:04

unom


1 Answers

You can't have multiple names for same action. It will be different actions. This is the way how mvc works. Mabe it's better to implement described behaviour with routing.

routes.MapRoute("Lang1RouteToController1Action1",
 "Lang1Controller/Lang1Action/{id}",
 new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);

routes.MapRoute("Lang2RouteToController1Action1",
 "Lang2Controller/Lang2Action/{id}",
  new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);

Ofcourse you'll have to create many routes, but you can make config file or store routing data in database, and just create them in loop on application start. Anyway I think it's better then creating planty of methods, becouse if you'll want to add one more language you'll need to find actions all over your controllers and recompile code. But in case of routes and config file - it become not so hard. Second thing is Html.ActionLink("Home", "Index", "Home") extension - you'll have to implement your own to return localized action link.

like image 60
er-v Avatar answered Oct 21 '22 19:10

er-v