临时数据和post方法的null会话(第二方法)Asp.net核心Web API

时间:2017-01-23 02:29:44

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

我,我试图在第二个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;
}

这是第一种保存数据的方法 First Method

This is the second method

Temp Data和session为null的第二种方法

0 个答案:

没有答案