C# ASP.NET MVC如何在Cookie中保存多语言

C# ASP.NET MVC如何在Cookie中保存多语言,c#,asp.net,asp.net-mvc,cookies,C#,Asp.net,Asp.net Mvc,Cookies,当使用更改语言并从Cookie读取语言时,我需要在Cookie中存储语言 下面的代码工作得很好,但我不知道在哪里以及如何将语言存储在cookie中并从cookie中读取 这是Rout: routes.MapRoute( name: "DefaultLocalized", url: "{language}/{controller}/{action}/{id}", defaults: new { controller = "

当使用更改语言并从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>
  • @Html.ActionLink(“英语”,ViewContext.RouteData.Values[“action”].ToString(),ViewContext.RouteData.Values[“controller”].ToString(),新的{language=“en-US”},null)
  • @ActionLink(“法语”,ViewContext.RouteData.Values[“action”].ToString(),ViewContext.RouteData.Values[“controller”].ToString(),new{language=“fr fr”},null)

  • 在Asp.Net中,您可以如下设置cookie:

    HttpContext.Current.Response.Cookies.Add(新的HttpCookie(“languageKey”、“languageValue”)

    并得到它:

    HttpCookie cultureCookie = HttpContext.Current.Request.Cookies["languageKey"];
    var cultureName = cultureCookie.Value;
    
    var languageValue = $.cookie("languageKey");
    
    JavaScript中,您可以将其用于JQuery设置cookie:

    $.cookie("languageKey", "yourValue"); 
    
    并得到它:

    HttpCookie cultureCookie = HttpContext.Current.Request.Cookies["languageKey"];
    var cultureName = cultureCookie.Value;
    
    var languageValue = $.cookie("languageKey");
    

    在Asp.Net中有一些方法可以使用它,但最简单的方法是您必须重写控制器的BeginExecute方法并在那里进行检查。

    此答案包含有关如何添加和读取您语言的cookies的信息。@KKAKURT,感谢您的帮助。我尝试了上述解决方案。不幸的是,我没有处理我的场景。我只需要在cookie中保存和读取与我的场景相匹配的语言。非常感谢。
        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;
        }