如何通过ActionLink使用资源?

发布于 2024-08-16 07:16:02 字数 78 浏览 2 评论 0原文

如何在ActionLink中整合资源?当我导航到注入文化语言的路线时,我希望标题显示翻译。但我不知道如何将翻译放入 ActionLink 中。

How to integrate resources in an ActionLink? I want the title to to display a translation when I navigate to a route where I inject the culture-language. But I do not know how to get the translation into the ActionLink.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

握住我的手 2024-08-23 07:16:02

也许我错过了一些东西。但您应该添加一条简单的路线,如下所示。

routes.MapRoute(
    "Default",                                              // Route name
    "{culture}/{controller}/{action}/{id}",                           // URL with parameters
    new { culture = "en-US", controller = "Home", action = "Index", id = "" }  // Parameter defaults
);

之后,您必须创建一个 ActionFilter 来根据请求设置区域性。

#region [ Imports ]

using System.Globalization;
using System.Threading;
using System.Web.Mvc;

#endregion

namespace SlideShowSample.Components
{


    public class CultureAttribute : ActionFilterAttribute, IActionFilter
    {

        #region IActionFilter Members

        void IActionFilter.OnActionExecuted(ActionExecutedContext filterContext) { }

        void IActionFilter.OnActionExecuting(ActionExecutingContext filterContext)
        {
            CultureInfo culture = new CultureInfo(filterContext.RouteData.GetRequiredString("Culture"));

            Thread.CurrentThread.CurrentCulture = culture;
            Thread.CurrentThread.CurrentUICulture = culture;
        }

        #endregion

    }

}

[Culture]
public class HomeController { }

最后在视图中使用ActionLink,如下所示。

<%= Html.ActionLink("Foo", "Foo", new { Culture = "en-GB" }) %>

上面的代码片段演示了一个简单的代码片段。您可以在此处找到更多信息
以及在 ASP.NET MVC 视图中使用资源的简单方法,在这里

Maybe I'm missing something. But you should add a simple route as follows.

routes.MapRoute(
    "Default",                                              // Route name
    "{culture}/{controller}/{action}/{id}",                           // URL with parameters
    new { culture = "en-US", controller = "Home", action = "Index", id = "" }  // Parameter defaults
);

Afterward, you have to create an ActionFilter to set the culture on request.

#region [ Imports ]

using System.Globalization;
using System.Threading;
using System.Web.Mvc;

#endregion

namespace SlideShowSample.Components
{


    public class CultureAttribute : ActionFilterAttribute, IActionFilter
    {

        #region IActionFilter Members

        void IActionFilter.OnActionExecuted(ActionExecutedContext filterContext) { }

        void IActionFilter.OnActionExecuting(ActionExecutingContext filterContext)
        {
            CultureInfo culture = new CultureInfo(filterContext.RouteData.GetRequiredString("Culture"));

            Thread.CurrentThread.CurrentCulture = culture;
            Thread.CurrentThread.CurrentUICulture = culture;
        }

        #endregion

    }

}

[Culture]
public class HomeController { }

At last in the view, use ActionLink as follows.

<%= Html.ActionLink("Foo", "Foo", new { Culture = "en-GB" }) %>

The above code snippet demonstrated a simple one. You can find more information here
and a simple way to use resources in ASP.NET MVC view, here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文