简体   繁体   中英

How to get request body data from System.Mvc.Web.ActionExecutingContext in c#

Here is my method It will take only one parameter . But I want user whaterver add in request body I need to save in logging.

[HttpPost]
[LogAPIUser]
public async Task<JsonResult> GameDetail(long game)

Here is my Postman request在此处输入图像描述

In ActionExecutingContext I have got only one action parameter

在此处输入图像描述

How can I get all body request data? If anyone have idea please let me know

Thanks in advance.

https://docs.microsoft.com/en-us/aspnet/web-api/overview/formats-and-model-binding/parameter-binding-in-aspnet-web-api states that:

At most one parameter is allowed to read from the message body.

The reason for this rule is that the request body might be stored in a non-buffered stream that can only be read once.

You can try to look at this question: WebAPI Multiple Put/Post parameters

  • basically, you read the whole body, and get your elements out of it.

something in the effect of

[HttpPost]
public string MyMethod([FromBody]JObject data)
{
    long game = data["game"].ToObject<Long>();
    long rer = data["rer"].ToObject<Long>();
}

(did not try code, might be buggy)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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