简体   繁体   English

在ASP.Net Core 2.1 MVC中,TempData始终为空

[英]TempData is always Null at ASP.Net Core 2.1 MVC

I would like to use TempData in my .net core mvc application. 我想在我的.net核心mvc应用程序中使用TempData。 I followed the article from https://docs.microsoft.com/en-us/aspnet/core/fundamentals/app-state?view=aspnetcore-2.1#tempdata 我按照https://docs.microsoft.com/en-us/aspnet/core/fundamentals/app-state?view=aspnetcore-2.1#tempdata上的文章进行了操作

I always get NULL Here is my code: 我总是得到NULL这是我的代码:

public async Task<ActionResult> Index(RentalsFilter filter)
{
    TempData["test"] = "ABC";
    return View();
}

public ActionResult Create()
{
    var abc = TempData["test"].ToString();
    return View();
}

Had similar issue due to GDRP ( https://docs.microsoft.com/en-us/aspnet/core/security/gdpr?view=aspnetcore-2.1 ). 由于GDRP( https://docs.microsoft.com/en-us/aspnet/core/security/gdpr?view=aspnetcore-2.1 )有类似的问题。 If you want have it up and running without worring about GDPR you can just disable it. 如果你想让它运行起来而不用担心GDPR你就可以禁用它。 The config below also uses cookies (default) instead of session state for TempData. 下面的配置也使用cookie(默认)而不是TempData的会话状态。

Startup.cs Startup.cs

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

        services.Configure<CookieTempDataProviderOptions>(options =>
        {
            options.Cookie.IsEssential = true;
        });

... ...

        app.UseHttpsRedirection();
        app.UseStaticFiles();
        app.UseCookiePolicy(); // <- this

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

Did you configure TempData as said in the doc: 您是否按照文档中的说明配置了TempData:

in ConfigureServices method add: 在ConfigureServices方法中添加:

services.AddMvc()
    .SetCompatibilityVersion(CompatibilityVersion.Version_2_1)
    .AddSessionStateTempDataProvider();

services.AddSession();

And in Configure method you should add: 在Configure方法中,您应该添加:

app.UseSession();

The answer that worked for me (for asp.net Core 2.2) was 对我有用的答案(对于asp.net Core 2.2)是

in Startup.Configure() app.UseCookiePolicy(); should be after app.UseMVC();

Which someone above has linked to in the comments from this stackoverflow answer 上面有哪些人在此stackoverflow答案的评论中链接到了

This is in addition to having 这是除了

app.UseSession() (in Configure) app.UseSession()(在配置中)

and

services.AddSession() (in ConfigureServices) services.AddSession()(在ConfigureServices中)

please place 请放置

@{TempData.Keep("test");}

in your HTML file. 在您的HTML文件中。 It will persist for the next request. 它会持续下一个请求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM