【问题标题】:Pass TempData between Contollers for Modal return Null?在模型返回 Null 的控制器之间传递 TempData?
【发布时间】:2019-07-07 08:39:37
【问题描述】:

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

但它对我不起作用,并且 TempData 返回 Null。它在这个控制器中工作正常。

    public async Task<IActionResult> LoginConfirm(LoginViewModel model)
    {
        ApplicationUser user = await userManager.FindByNameAsync(model.Email);
        if (user!=null)
        {
            var status = await signInManager.PasswordSignInAsync(user, model.Pass,model.RememberMe,true);
            if (status.Succeeded)
            {
                TempData["msg"] = "You Login successful ";
                return RedirectToAction("Index","Home");
            }
        }
        TempData["msg"] = "Somethings Wrong!";

        return View("Login");
    }

【问题讨论】:

  • 请出示 StartUp 类
  • 这是我的启动类:

标签: asp.net-mvc controller tempdata


【解决方案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();

之后

【讨论】:

  • 哦。感谢它现在的工作 Farhad
  • 祝你好运... ;)
【解决方案2】:
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?}");
        });
    }
}

}

【讨论】:

    猜你喜欢
    • 2015-01-21
    • 2010-12-31
    • 2012-11-16
    • 1970-01-01
    • 2018-06-06
    • 2023-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多