2017-02-01 27 views
1

イントラネットアプリケーションにグローバリゼーションを追加しようとしています。ユーザーにカルチャーの設定を許可するためにクッキーを使用しています。ミドルウェアが設定され、実行されていますが、私はUI選択に基づいてクッキーに追加することで問題に遭遇しました。ASP.Net Core 1.1でResponse.Cookies.Append()を実行する方法は?

方法は、以下のようにAsp.Netコアのドキュメントからまっすぐです:は

  • LocalRedirectが
  • 存在しない

    1. 応答が存在しません:

      public void ConfigureServices(IServiceCollection services) 
      { 
          services.Configure<RequestLocalizationOptions>(
           options => 
           { 
            var supportedCultures = new List<CultureInfo> 
            { 
            new CultureInfo("en-US"), 
            new CultureInfo("en-GB"), 
            new CultureInfo("fr-FR"), 
            new CultureInfo("es-ES") 
            }; 
      
            options.DefaultRequestCulture = new RequestCulture(culture: "en-GB", uiCulture: "en-GB"); 
            options.SupportedCultures = supportedCultures; 
            options.SupportedUICultures = supportedCultures; 
           }); 
      
          services.AddLocalization(); 
          services.AddMvc(config => 
          { 
           var policy = new AuthorizationPolicyBuilder() 
               .RequireAuthenticatedUser() 
               .Build(); 
           config.Filters.Add(new AuthorizeFilter(policy)); 
          }) 
          .AddViewLocalization(); 
      
          services.AddSession(options => { 
           options.CookieName = "Intranet"; 
          }); 
      } 
      
      public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) 
      { 
          var locOptions = app.ApplicationServices.GetService<IOptions<RequestLocalizationOptions>>(); 
          app.UseRequestLocalization(locOptions.Value); 
      
          app.UseSession(); 
          app.UseMvc(routes => 
          { 
           routes.MapRoute(
            name: "default", 
            template: "{controller=Home}/{action=Index}/{id?}"); 
          }); 
      } 
      
      [HttpPost] 
      public IActionResult SetLanguage(string culture, string returnUrl) 
          { 
          Response.Cookies.Append(
           CookieRequestCultureProvider.DefaultCookieName, 
           CookieRequestCultureProvider.MakeCookieValue(new RequestCulture(culture)), 
           new CookieOptions { Expires = DateTimeOffset.UtcNow.AddYears(1) 
          }); 
      
          return LocalRedirect(returnUrl); 
      } 
      

      問題があります

      私は試しました:

      あなたがCookieRequestCultureProviderを使用するようにしたすべての
      1. のHttpResponse、HttpRequestの
      2. LocalRedirectResult
    +0

    あなたは内部にそのコードを持っていますかコントローラーまたは別の場所でスタンドアロンになっていますか? – DavidG

    +0

    [Cookies and ASP.NET Core]の可能な複製(https://stackoverflow.com/questions/36166075/cookies-and-asp-net-core) –

    答えて

    3

    、あなたはコードはサンプルプロジェクトがたくさんGitHubから来ていることがわかります。この特定のサンプルはLocalization.StarterWebからのものです。

    あなたの二つの「行方不明」の方法は何Controller継承からである(実際にはControllerBaseの一部である。あなたは、コントローラには、このアクションメソッドを置けば、それは動作しますので。

    public class HomeController : Controller 
    { 
        [HttpPost] 
        public IActionResult SetLanguage(string culture, string returnUrl) 
        { 
         Response.Cookies.Append(
          CookieRequestCultureProvider.DefaultCookieName, 
          CookieRequestCultureProvider.MakeCookieValue(new RequestCulture(culture)), 
          new CookieOptions { Expires = DateTimeOffset.UtcNow.AddYears(1) } 
         ); 
    
         return LocalRedirect(returnUrl); 
        } 
    } 
    
    +0

    コントローラ...クラスではありません。それは愚かな間違いだった、ありがとう! – K7Buoy

    0

    まず。後の例では、この例のアクションはうまくいくはずです。 私もこれを追加します。

    CultureInfo.CurrentCulture = culture; 
    CultureInfo.CurrentUICulture = culture; 
    

    は、ここに私の設定です:あなたはそのサンプルを得たドキュメントから

    public void ConfigureServices(IServiceCollection services) 
    { 
        services.AddLocalization(opts => { opts.ResourcesPath = "Resources"; }); 
    
        services.AddMvc() 
         .AddViewLocalization(
          Microsoft.AspNetCore.Mvc.Razor.LanguageViewLocationExpanderFormat.SubFolder, 
          opts => { opts.ResourcesPath = "Resources"; } 
         ) 
         .AddDataAnnotationsLocalization(); 
    
        services.Configure<RequestLocalizationOptions>(opts => 
        { 
         var supportedCultures = new[] 
         { 
          new CultureInfo("en-US"), 
          ...    
         }; 
         opts.DefaultRequestCulture = new Microsoft.AspNetCore.Localization.RequestCulture("en-US"); 
         opts.SupportedCultures = supportedCultures; 
         opts.SupportedUICultures = supportedCultures; 
        }); 
    } 
    
    public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) 
    { 
        app.UseRequestLocalization(); 
    
        app.UseMvc(routes => 
        { 
         routes.MapRoute(name: "default", template: "{controller=Home}/{action=Index}"); 
        }); 
    } 
    
    +1

    これは答えません質問は全くありません – DavidG

    +0

    Response.Cookies.Append()を実行する必要はありません。 –

    +0

    あなたが提供したコードは、ユーザーがカルチャーpをどのように切り替えることができるのでしょうか?リファレンス? – DavidG

    関連する問題