【问题标题】:TempData Return NullTempData 返回空值
【发布时间】:2020-02-19 16:36:59
【问题描述】:

我正在初始化 Tempdata 我需要在另一个操作中检索临时数据,但它返回 null。

    public  IActionResult GetRestaurants(int? id)
    {
        TempData["HotelID"] = id;

        return Ok();
    }
    [HttpPost]
    public IActionResult AddRestaurant()
    {

      int x =int.Parse(TempData["HotelID"].ToString());

    }

【问题讨论】:

  • 你甚至没有发布OK()的代码...
  • 我试了一下还是返回null
  • 你的项目是直接在3.0版本创建的还是从2.x迁移到3.0的?我用 3.0 版对你的代码进行了测试,但我得到了 tempdata 的值。你能分享一个可以重现问题的演示吗?
  • 3.0版本直接创建的项目

标签: asp.net-core asp.net-core-mvc


【解决方案1】:

startup.cs的ConfigureServices方法:

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

startup.cs的配置方法

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
                app.UseHsts();
            }

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

            app.UseHttpsRedirection();
            app.UseStaticFiles();
            app.UseCookiePolicy();
        }

更多详情请访问Session and app state in ASP.NET Core

Index1 动作方法

 public IActionResult Index()
            {
                Message = $"Customer abcd added";
                TempData["name"] = "Test data";
                TempData["age"] = 30;
                TempData.Keep();


               // Session["name"] = "Test Data";

                return View();
            }

index2 动作方法

 public IActionResult About()
        {

           var userName = TempData.Peek("name").ToString();


           var userAge = int.Parse(TempData.Peek("age").ToString());
            return View();
        }

【讨论】:

  • 你能把你的startup.cs放进去吗?
  • public void ConfigureServices(IServiceCollection services) { services.AddMemoryCache();服务.AddSession(); services.AddMvc().AddNewtonsoftJson(options => { options.SerializerSettings.ContractResolver = new DefaultContractResolver(); }); services.AddAutoMapper(); services.AddDbContext(options => options.UseSqlServer(Configuration.GetConnectionString("Default"))); }
  • public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage();} else{ app.UseExceptionHandler("/Home/Error");应用程序.UseHsts(); } app.UseSession(); app.UseCors(MyAllowSpecificOrigins); app.UseRouting(); app.UseAuthorization(); app.UseEndpoints(endpoints => { endpoints.MapControllerRoute(name: "default", pattern: "{controller=Home}/{action=Index}/{id?}"); }); app.UseHttpsRedirection(); app.UseStaticFiles(); app.UseCookiePolicy(); }
  • 你使用的asp.net core是什么版本的?
  • 如果是2.x范围则需要设置可比版本services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.your version)
猜你喜欢
  • 2012-07-25
  • 2021-07-07
  • 1970-01-01
  • 2021-10-06
  • 2021-07-04
  • 2021-12-21
  • 2015-11-07
  • 2020-05-20
相关资源
最近更新 更多