在控制器之间传递TempData以获得模态返回Null?

时间:2019-07-07 08:39:37

标签: asp.net-mvc controller tempdata

我想在两个控制器之间传递一些字符串以显示使用模式的成功登录。我阅读了以下主题: ViewBag, ViewData and TempDataRedirectToAction with parameter

但是它对我不起作用,TempData返回Null。它在此Controller中工作正常。

{{1}}

2 个答案:

答案 0 :(得分:1)

您有两种选择

1) 当您使用

services.Configure<CookiePolicyOptions>(options =>
        {
            // This lambda determines whether user consent for non-essential cookies is needed for a given request.
            options.CheckConsentNeeded = context => true;
            options.MinimumSameSitePolicy = SameSiteMode.None;
        });

您启用了GDPR(通用数据保护法规),因此只要用户不接受您的cookie,您将无法在站点中设置cookie。这会使TempData为空。

2) 迁移到ASP Core 2.1之后,出现了这个问题,并在工作了一天后找到了解决方案:

在Startup.Configure()app中的

.UseCookiePolicy();应该在app.UseMVC();

之后

答案 1 :(得分:0)

namespace GiftSite
{
    public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
        services.Configure<CookiePolicyOptions>(options =>
        {
            // This lambda determines whether user consent for non-essential cookies is needed for a given request.
            options.CheckConsentNeeded = context => true;
            options.MinimumSameSitePolicy = SameSiteMode.None;
        });
        services.AddAuthentication();
        services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        else
        {
            app.UseExceptionHandler("/Home/Error");
            // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
            app.UseHsts();
        }

        app.UseHttpsRedirection();
        //app.UseHttpMethodOverride();
        app.UseStaticFiles();
        app.UseCookiePolicy();
        app.UseAuthentication();
        app.UseMvc(routes =>
        {
            routes.MapRoute(
                name: "default",
                template: "{controller=Home}/{action=Index}/{id?}");
        });
    }
}

}

相关问题