简体   繁体   English

两种方法POST和GET之间的WEB API会话处理

[英]WEB API session handling between 2 methods POST and GET

I am trying to Save the model data in HttpContext.Current.Session["TestModel"] in on the POST method and trying to retrieve the same model data from HttpContext.Current.Session["TestModel"] in GET method. 我试图在POST方法中将模型数据保存在HttpContext.Current.Session["TestModel"]中,并尝试从GET方法中从HttpContext.Current.Session["TestModel"]检索相同的模型数据。 Below is the code what I am trying: 下面是我正在尝试的代码:

[HttpPost]
public async Task<HttpResponseMessage> UploadDataJson(HttpRequestMessage request)
{

    var session = HttpContext.Current.Session;
    OasisSessionModel oasissessionmodel = new OasisSessionModel(); //I tried setting it as a global
    oasissessionmodel.TotalRows = 200;
    HttpContext.Current.Session["TestModel"] = oasissessionmodel;
    return someresponse;
}

[HttpGet]
[Route("api/GetUploadUpdates")]
public IHttpActionResult GetUploadUpdates()
{
    var session = HttpContext.Current.Session;
    OasisSessionModel oasissessionmodel = null;
    oasissessionmodel = HttpContext.Current.Session["TestModel"] as OasisSessionModel;
    return Ok();
}

But in this case HttpContext.Current.Session["TestModel"] is always null , is there any other way to handle session Globally in WEB API. 但是在这种情况下, HttpContext.Current.Session["TestModel"]始终为null ,是否还有其他方法可以在WEB API中全局处理会话。 Thanks in advance 提前致谢

Have a look at this answer: link 看看这个答案: 链接

This might be a solution to your problem, are you using .NET to access the API (MVC, etc.)? 这可能是解决您的问题的方法,您是否正在使用.NET访问API(MVC等)?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM