Passing TempData with RedirectToAction

前端 未结 5 1524
夕颜
夕颜 2021-02-13 15:27

Intro: I am a .NET studet trying to learn ASP.NET Core MVC. So please be understanding. I have searched the web for an answer to my problem, but havent found a solution that wor

5条回答
  •  情书的邮戳
    2021-02-13 16:10

    I landed on this question while googling for "asp.net core redirect to action tempdata". I found the answer and am posting it here for posterity.

    Problem

    My issue was that, after filling in some TempData values and calling RedirectToAction(), TempData would be empty on the page that I was redirecting to.

    Solution

    Per HamedH's answer here: If you are running ASP.NET Core 2.1, open your Startup.cs file and make sure that in your Configure() method app.UseCookiePolicy(); comes after app.UseMVC();.

    Example:

    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        ...
    
        app.UseMvc(routes =>
        {
            routes.MapRoute(
                name: "default",
                template: "{controller=Home}/{action=Index}/{id?}");
            });
        }
    
        app.UseCookiePolicy();
    }
    

提交回复
热议问题