获取当前的动作和控制器,并使用它作为在Html.ActionLink变量? [英] Get current action and controller and use it as a variable in an Html.ActionLink?

查看:110
本文介绍了获取当前的动作和控制器,并使用它作为在Html.ActionLink变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要能够动态地检索你在任何网页当前的动作和控制器的名称,并实际使用它们来创建一个新的HTML.ActionLink链接到同一个动作和控制器的名称,但在不同的区。所以我想我需要检索当前的动作和控制器的名称作为变量来建立一个新的HTML.ActionLink使用。

I need to be able to dynamically retrieve the current action and controller name of whatever page you're on, and actually use them to create a new HTML.ActionLink that links to the same action and controller name, but in a different area. So I guess I need to retrieve the current action and controller name as variables to use in building a new HTML.ActionLink.

所以,如果我是www.site.com/about页面上,我需要有动态生成的www.site.com/es/about页面的链接。

So, if I'm on the www.site.com/about page, I need to have a link dynamically generated to the www.site.com/es/about page.

我基本上建在一个单独的区域文件夹我的网站的西班牙语翻译版本(具有相同的命名控制器和动作和看法,只是内容全是西班牙语)。我需要的用户可以来回切换页面的英文版本(这是默认设置,并驻留在根网站的观点)和西班牙版(其观点驻留在该地区的文件夹ES)之间哪个页面他们是目前。我不能硬code这些链接,因为我需要这其中是我_layout每一页上使用的_topNavigation共享的局部视图。

I've basically built a spanish translated version of my site in a separate area folder (with the same named controllers and actions and views, just content is all in spanish). I need the user to be able to toggle back and forth between the english version of the page (which is the default, and resides in the root site views) and the spanish version (whose views resides in the area folder "es") of whichever page they're currently on. I can't "hardcode" these links because I need this in a shared partial view which is the _topNavigation in my _Layout used on every page.

请让我知道如果我需要澄清。我敢肯定,用区是不是真的本地化应用程序时的路要走,但我还是努力地教自己asp.net MVC。我读了本地化很多很多的教程和例子,我可以只是没有让他们的工作或意义。

Please let me know if I need to clarify. I'm sure using "areas" wasn't really the way to go when localizing an application, but I'm still trying hard to teach myself asp.net MVC. I read many MANY tutorials and examples on localization, and I could just not get them to work or make sense.

我还要补充一点,我已经知道如何使用HTML.ActionLink去来回区域之间。我已经成功地创建正确的HTML.ActionLinks任何在西班牙语(ES)区的意​​见,以及任何在默认站点的意见。所以这不是我的问题。

I should also add that I already know how to use HTML.ActionLink to go back and forth between the areas. I've managed to create the correct HTML.ActionLinks to any of the views in the spanish (es) area, and to any of the views in the default site. So that is not my question.

任何帮助是极大AP preciated!谢谢!

Any help is greatly appreciated! Thanks!

推荐答案

使用 ViewContext.RouteData 来确定当前的控制器和行动:

Use ViewContext.RouteData to determine current Controller and Action:

@Html.ActionLink("Spanish Version", 
                 ViewContext.RouteData.Values["action"] as String,
                 ViewContext.RouteData.Values["controller"] as String,
                 new { Area = "es" })

这篇关于获取当前的动作和控制器,并使用它作为在Html.ActionLink变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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