如何在ASP.Net Core 1.1中使用Response.Cookies.Append()? [英] How to Response.Cookies.Append() in ASP.Net Core 1.1?

查看:486
本文介绍了如何在ASP.Net Core 1.1中使用Response.Cookies.Append()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用cookie允许用户具有文化偏爱,将全球化添加到Intranet应用程序中。中间件已设置并正在运行,但是我遇到了基于UI选择将Cookie追加到Cookie的问题。

I am trying to add Globalization to an Intranet application, using a cookie to allow users a culture preference. The middleware is set up and running but I have run into an issue with appending to the cookie based on the UI selection.

该方法直接来自Asp.Net Core文档如下:

The method is straight from the Asp.Net Core documentation as below:

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);
 }

问题是:


  1. 响应不存在

  2. LocalRedirect不存在

我尝试过:


  1. HttpResponse,HttpRequest

  2. LocalRedirectResult


推荐答案

从获得该示例的文档中,您可以看到代码来自 GitHub ,其中包含许多示例项目。此特定示例来自 Localization.StarterWeb

From the docs where you got that sample, you can see that the code comes from GitHub with lots of sample projects. This particular sample comes from Localization.StarterWeb.

您的两个缺失方法实际上是 ControllerBase 的一部分(这就是 Controller 继承自。因此,如果将此操作方法放入控制器中,它将起作用。

Your two "missing" methods are actually part of ControllerBase (which is what Controller inherits from. So if you put this action method into a controller, it will work.

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);
    }
}

这篇关于如何在ASP.Net Core 1.1中使用Response.Cookies.Append()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆