ASP.NET MVC如何在Cookies中保存多语言

时间:2017-05-19 08:21:58

标签: c# asp.net asp.net-mvc cookies

我需要在使用时更改语言并从Cookie中读取语言时将语言存储在Cookie中 以下代码效果很好,但我不知道在哪里以及如何将语言存储在cookie中以及从cookie中读取

这是Rout:

    routes.MapRoute(
      name: "DefaultLocalized",
      url: "{language}/{controller}/{action}/{id}",
      defaults: new
      {
          controller = "Home",
          action = "Index",
          id = UrlParameter.Optional,


      }
          );

ActionFilterAttribute:

    public class EnableMultiLanguage : ActionFilterAttribute
    {

        public EnableMultiLanguage()
        {
        }


        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            string language = (string)filterContext.RouteData.Values["language"];
            if (!string.IsNullOrEmpty(language))
            {

                var cultureInfo = CultureInfo.GetCultures(CultureTypes.AllCultures).FirstOrDefault(x => x.Name == language);

                if (cultureInfo == null)
                {
                    cultureInfo = new CultureInfo("en-US");
                }

                Thread.CurrentThread.CurrentCulture = cultureInfo;
                Thread.CurrentThread.CurrentUICulture = cultureInfo;
                filterContext.RouteData.Values["language"] = cultureInfo.Name;

            }
            else
            {
                Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
                Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-US");
                filterContext.RouteData.Values["language"] = "en-US";
            }
        }
    }
}

以下是用户如何更改语言:

   <li>@Html.ActionLink("English", ViewContext.RouteData.Values["action"].ToString(), ViewContext.RouteData.Values["controller"].ToString(), new { language = "en-US" }, null)</li>
   <li>@Html.ActionLink("French", ViewContext.RouteData.Values["action"].ToString(), ViewContext.RouteData.Values["controller"].ToString(),new { language = "fr-FR" },null)</li>

2 个答案:

答案 0 :(得分:0)

Asp.Net 中,您可以像这样设置Cookie:

HttpContext.Current.Response.Cookies.Add(new HttpCookie("languageKey", "languageValue"));

得到它:

HttpCookie cultureCookie = HttpContext.Current.Request.Cookies["languageKey"];
var cultureName = cultureCookie.Value;

并且在 JavaScript 中,您可以通过JQuery将其用于set cookie:

$.cookie("languageKey", "yourValue"); 

得到它:

var languageValue = $.cookie("languageKey");

有一些方法可以在Asp.Net中使用它,但简单的一种方法是你必须覆盖控制器的 BeginExecute 方法并在那里检查它。

答案 1 :(得分:0)

    public void OnAuthorization(AuthorizationContext filterContext)
    {
        var routes = filterContext.RouteData.Values;

        string culture = (routes["culture"] ?? defaultCulture).ToString();

        if (!filterContext.HttpContext.Request.Cookies.AllKeys.Contains("culture"))
        {
            HttpCookie StudentCookies = new HttpCookie("culture");
            StudentCookies.Value = culture;
            StudentCookies.Expires = DateTime.Now.AddYears(1);
            filterContext.HttpContext.Response.Cookies.Add(StudentCookies);
        }
        else
        {
            if (filterContext.HttpContext.Request.Cookies["culture"].Value != (routes["culture"] ?? "").ToString() && routes["culture"] != null)
            {
                filterContext.HttpContext.Response.Cookies["culture"].Value = (routes["culture"] ?? "").ToString();
            }
            if (routes["culture"] == null || routes["culture"] == UrlParameter.Optional)
            {
                culture = filterContext.HttpContext.Request.Cookies["culture"].Value;
            }
        }

        HelperClass.SetCulture(culture);


        if (!routes.ContainsKey("culture") || routes["culture"] == UrlParameter.Optional)
        {
            filterContext.Result = new RedirectResult("/" + culture + "/" + filterContext.HttpContext.Request.Url.LocalPath);
        }

        CultureInfo info = new CultureInfo(culture);
        Thread.CurrentThread.CurrentCulture = info;
        Thread.CurrentThread.CurrentUICulture = info;
        return;
    }