Passing TempData with RedirectToAction

前端 未结 5 1497
夕颜
夕颜 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:14

    Did you configure Session? TempData is using session behind the scenes.

    Project.json

    "Microsoft.AspNetCore.Session": "1.1.0"
    

    Here is the Startup.cs file. - ConfigureServices method

    public void ConfigureServices(IServiceCollection services)
    {
         services.AddMemoryCache();
         services.AddSession();
         services.AddMvc();
    }
    

    And Configure method.

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

    Now try with TempData, it will work.

    And you can set the environment with set ASPNETCORE_ENVIRONMENT=Development environment variable.

提交回复
热议问题