You can simply use the default model binding , create a class and application will automatically bind to object after getting the post request :
[HttpPost]
public void Post([FromBody] MyValue value)
{
}
public class MyValue {
public int projectID { get; set; }
//Other property
}
And the request will be something like :
POST https://localhost:XXXX/api/values
Content-Type: application/json
{"projectID": 1}
Then you can save to session for later access if you want the data available across requests.
Or you can just not use model binding(remove [FromBody] parameter , model binding execute before filter) , and get the request body in OnActionExecuting and save to session for later access :
public class MyControllerBase: Controller
{
public override void OnActionExecuting(ActionExecutingContext context)
{
base.OnActionExecuting(context);
var bodyStr = "";
var req = context.HttpContext.Request;
// Allows using several time the stream in ASP.Net Core
req.EnableRewind();
// Arguments: Stream, Encoding, detect encoding, buffer size
// AND, the most important: keep stream opened
using (StreamReader reader
= new StreamReader(req.Body, Encoding.UTF8, true, 1024, true))
{
bodyStr = reader.ReadToEnd();
}
// Rewind, so the core is not lost when it looks the body for the request
req.Body.Position = 0;
}
}
And you can make your controller inherits MyControllerBase and modify that to meet your requirement .