从C#WebAPI中的HTTP GET中读取内容正文 [英] Read content body from a HTTP GET in C# WebAPI

查看:1182
本文介绍了从C#WebAPI中的HTTP GET中读取内容正文的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个WebAPI C#应用程序。我的GET方法被定义为:

  [HttpGet] 
public HttpResponseMessage Get(string id)

该API根据给定的 id 。另一个参数是必需的,但它是很长的,它在URL上不起作用,所以我使用GET主体发送这样的第二个参数。



如何可以我从get方法中检索它?



我试过了

  var dataOnBody = await Request.Content.ReadAsStringAsync(); 

但它不起作用获取方法不是 async ,我认为它不需要是那个(我想要一个正常的阻塞函数来读取正文的内容并输出一个字符串) p>

我只需要一个简单的方法来从请求体中提取我的字符串

解决方案

<即使你设法做到这一点,你也会发现这种支持并不普遍。 HTTP规范说:


GET方法表示检索任何信息(以
实体的形式)由Request-URI标识。


因此,返回的数据仅依赖于URI,而不依赖于正文中的任何内容。许多库甚至不会让你在GET期间发送请求体。


I have a WebAPI C# application. My GET method is defined as:

[HttpGet]
public HttpResponseMessage Get(string id)

This API retrieves some content from a database, based on a given id. Another parameter is required but it is so long that having it on the URL would not work, so I'm using the GET body to send such second parameter.

How can I retrieve it from inside the get method?

I tried

var dataOnBody = await Request.Content.ReadAsStringAsync();

but it doesn't work as the Get method is not async and I think it doesn't need to be that (I want a normal blocking function which reads the content of the body and outputs a string)

I just need a simple way to extract my string from the request body

解决方案

Even if you somehow manage to do this, you will find that support is not universal. The HTTP specs say:

The GET method means retrieve whatever information (in the form of an entity) is identified by the Request-URI.

So the data returned relies only on the URI, not anything in the body. Many libraries won't even let you send a request body during a GET.

这篇关于从C#WebAPI中的HTTP GET中读取内容正文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆