ASP.NET MVC - 我能有同样的动作多个名字? [英] ASP.NET MVC - Can i have multiple names for the same action?

查看:118
本文介绍了ASP.NET MVC - 我能有同样的动作多个名字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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?

我要寻找一个完整的多语言解决方案。本质上,我想所有的逻辑是SA相同,但是改变关键词(行动,在url控制器),取决于语言。

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.

推荐答案

您不能有相同的动作多个名称。这将是不同的动作。这是怎样的方式MVC的作品。马贝它更好地实现与路由描述的行为。

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,你必须创造出许多的路线,但你可以在数据库使配置文件或存储路由数据,只是在应用程序启动循环创建它们。无论如何,我认为这是更好然后创建方法普朗蒂,becouse如果你要添加更多的语言,你需要找到所有的动作在你的控制器和重新编译code。但是,在路线和配置文件的情况下 - 它变得不那么辛苦。
第二件事是Html.ActionLink(家,索引,家)扩展 - 你要实现自己的返回本地化的操作链接

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.

这篇关于ASP.NET MVC - 我能有同样的动作多个名字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆