[英]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.