我,我试图在第二个httppost方法的ASP.NET核心中使用TempData,TempData值为null。我做了很多研究,但无法解决问题我在TempData和会话中遇到空值
这是 project.json 文件
"Microsoft.AspNetCore.Session": "1.1.0",
"Microsoft.Extensions.Caching.Memory": "1.1.0"
startup.cs
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
services.AddMemoryCache();
services.AddDistributedMemoryCache();
services.AddSession();
services.AddSingleton<ITempDataProvider, CookieTempDataProvider>();
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
app.UseSession();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
}
Market.cs
[HttpGet("{pageNumber}")]
public GenericResponseObject<List<MarketViewModel>> GetMarketList(int pageNumber)
{
TempData["Currentpage"] = pageNumber; --> value is set here
HttpContext.Session.SetString("Currentpage", pageNumber);
return genericResponseObject;
}
[HttpPost]
public GenericResponseObject<List<MarketViewModel>> AddUpdateMarket([FromBody] MarketViewModel marketViewModel)
{
GenericResponseObject<List<MarketViewModel>> genericResponseObject = new GenericResponseObject<List<MarketViewModel>>();
genericResponseObject.IsSuccess = false;
string name = HttpContext.Session.GetString("Currentpage"); --> the value is null here
genericResponseObject = GetMarketList(Convert.ToInt16(TempData.Peek("Currentpage")));
return genericResponseObject;
}
Temp Data和session为null的第二种方法