开发者

How to use resources with ActionLink?

How to integrate 开发者_JS百科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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜