简体   繁体   English

如何从ASP.NET Core中的Request.Body中读取参数

[英]How to read parameter from Request.Body in ASP.NET Core

Context: My application is behind a central login app, whenever the user apply access to my application, my application got a http request contain the user info. 上下文:我的应用程序位于中央登录应用程序的后面,每当用户对我的应用程序应用访问时,我的应用程序都会收到一个包含用户信息的http请求。 And I need to retrieve the user info from the HttpRequest Body. 我需要从HttpRequest正文中检索用户信息。

This is what I tried so far: 这是我到目前为止尝试过的:

currentContext.HttpContext.Request.Query["user-name"].toString();  // got nothing

using (var reader = new StreamReader(currentContext.HttpContext.Request.Body))
{
    var body = reader.ReadToEnd();
}   // I can get the raw HttpRequest Body as "user-name=some&user-email=something"

Is there any method I can use to parse the parameters and values from the Request.Body? 有什么方法可以用来解析Request.Body中的参数和值? I tried the following, got nothing either. 我尝试了以下方法,但一无所获。

HttpContext.Item['user-name'] \\\\return nothing Request.Form["user-name"] \\\\ return nothing

and the reason I am not be able to use model binding is, in the HttpRequest body, the key name is "user-name", and in c#, I can't create a variable with a "-" 我无法使用模型绑定的原因是,在HttpRequest正文中,键名是“用户名”,而在c#中,我无法使用“-”创建变量

Meanwhile, in the my .net 4.6 application, Request["KeyName"].toString() works just fine. 同时,在我的.net 4.6应用程序中, Request["KeyName"].toString()可以正常工作。

I figured out a way to convert the raw HttpRequest Body to a Query String, then read parameters from it. 我想出了一种方法,可以将原始HttpRequest主体转换为查询字符串,然后从中读取参数。

Here is the code: 这是代码:

var queryString = Microsoft.AspNetCore.WebUtilities.QueryHelpers.ParseQuery(requestBody);

string paramterValueIWant = queryString["KeyName"];

There is one problem though, when the KeyName doesn't exist in the body, it will throw an exception. 但是,有一个问题,当主体中不存在KeyName时,它将引发异常。 So you have to null check or do a try catch. 因此,您必须进行null检查或尝试捕获。

Still I feel like there should be a better way to read the parameter, as I mentioned, in my .net 4.6 application, all I need to do is Request["KeyName"] . 我仍然觉得应该有一种更好的方法来读取参数,正如我提到的那样,在我的.net 4.6应用程序中,我需要做的只是Request["KeyName"]

Assuming that we are talking about POST/PUT/PATCH call, you can use 假设我们正在谈论POST / PUT / PATCH调用,则可以使用
Request.Form["KeyName"] in your API method and set the 'contentType' of the Ajax request as application/x-www-form-urlencoded 您的API方法中的Request.Form["KeyName"]并将Ajax请求的'contentType'设置为application/x-www-form-urlencoded
Notice that Request is automagically available inside your method. 注意, Request在方法内部自动可用。 No need to explicit call it. 无需显式调用它。

When using GET/DELETE call i prefer to use 使用GET / DELETE通话时,我更喜欢使用

[HttpGet("{UserId}")] // api/User/{UserId}
public IActionResult Get(int UserId){
  // do stuff calling directly UserId
}

Or with PUT/PATCH 或搭配PUT / PATCH

[Route("User/{EntityId}/{StatusFilter}")] // api/User/{EntityId}/{StatusFilter}
[HttpPut] 
public IActionResult Put(int EntityId, int StatusFilter){
  // do stuff calling directly EntityId and StatusFilter
}

where you can then still take data from the Body using Request.Form["KeyName"] 然后您仍然可以使用Request.Form["KeyName"]从主体获取数据

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

相关问题 在 ASP.NET Core FromHeader 导致 Request.Body 被完全读取 - In ASP.NET Core FromHeader is causing the Request.Body to be fully read 从 ASP.NET Core 中的 Request.Body 读取数据时发生 SocketException - SocketException while reading data from Request.Body in ASP.NET Core asp.net 核心 1.0 mvc。 从 Request.Body 获取原始内容 - asp.net core 1.0 mvc. Get raw content from Request.Body 如何大摇大摆地记录一个直接读取 Request.Body 的 ASP.NET 核心操作 - How to swagger-document an ASP.NET Core action which reads Request.Body directly 如何在 asp.net core webapi 控制器中读取请求正文? - How to read request body in an asp.net core webapi controller? 如何从 ASP.NET 核心中的 JSON 请求正文中读取多个参数? - How do I read multiple parameters from a JSON request body in ASP.NET Core? 如何使 .NET 4.8 中的 Request.Content 在.Net Core 中工作? 或者 Request.Body 从.Net Core 到 .NET 4.8? - How to make Request.Content from .NET 4.8 to work in .Net Core? Or Request.Body from .Net Core to .NET 4.8? 在 ASP.NET 核心中重新读取请求正文 - Re-read a Request body in ASP.NET core ASP.NET Core无法读取请求正文 - ASP.NET Core Cannot Read Request Body 如何重置 PipeReader 以便能够在 ASP.NET Core 中重新读取请求正文 - How to reset PipeReader to be able to re-read request body in ASP.NET Core
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM