简体   繁体   English

如何在 c# 中从 System.Mvc.Web.ActionExecutingContext 获取请求正文数据

[英]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.但我希望用户 whaterver 添加我需要保存在日志记录中的请求正文。

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

Here is my Postman request这是我的邮递员请求在此处输入图像描述

In ActionExecutingContext I have got only one action parameter在 ActionExecutingContext 我只有一个动作参数

在此处输入图像描述

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: https://docs.microsoft.com/en-us/aspnet/web-api/overview/formats-and-model-binding/parameter-binding-in-aspnet-web-api指出:

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你可以试试看这个问题: 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) (没有尝试代码,可能是错误的)

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

相关问题 如何从 ActionExecutingContext 获取路由模板 - how to get a template of route from ActionExecutingContext 我已经设置了Web服务,但不知道如何在MVC 5 C#中从中获取数据 - I have set up a web service but have no idea how to get data from it in MVC 5 c# 如何从任务中获取请求正文和响应正文<HttpResponseMessage> C#中的SendAsync登录数据库 - How to get request body and response body from Task<HttpResponseMessage> SendAsync in C# to log in DB 当从c#MVC中的外部系统调用Web API时,获取视图中的通知 - Get notification in view when a web API called from an external system in c# MVC 如何从C#MVC中的记录获取数据 - How do i get data from records in C# MVC 如何从json中获取数据到MVC4 C#? - How to get the data from json to MVC4 c#? C#如何将数据列表从视图中获取到MVC 3中的控制器 - C# how to get list of data from view into controller in MVC 3 从ActionExecutingContext获取原始端口? - Get originating port from ActionExecutingContext? 如何在 IActionFilter Asp.net core 中读取 OnActionExecuting(ActionExecutingContext context) 中的请求正文 - How to read request body in OnActionExecuting(ActionExecutingContext context) in IActionFilter Asp.net core 如何使用C#发送包含JSON正文的GET请求? - How to send a GET request containing a JSON body, using C#?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM