无法在ASP.NET WebApi控制器中读取Request.Content
我在TransferMode.Streamed HttpSelfHostConfiguration exe中使用WebApi编写代理。
当我使用fiddler发布到我的ApiController时,出于某种原因我无法读取Request.Content – 即使我有POSTed数据它也返回“”
public class ApiProxyController : ApiController { public Task Post(string path) { return Request.Content.ReadAsStringAsync().ContinueWith(s => { var content = new StringContent(s.Result); //s.Result is "" CopyHeaders(Request.Content.Headers, content.Headers); return Proxy(path, content); }).Unwrap(); } private Task Proxy(string path, HttpContent content) { ... } }
这是我的网络请求
POST https://localhost:3001/api/values HTTP/1.1 Host: localhost:3001 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 Content-Type: application/json Content-Length: 26 { "text":"dfsadfsadfsadf"}
我做错了什么? 为什么s.Result作为空字符串而不是原始json返回?
我也很挣扎。 ReadAsStringAsync
和ReadAsAsync
返回任务对象。 引用Result
属性将返回内容。 它可能是引用Result属性导致异步读取请求被阻止。
例:
string str = response.Content.ReadAsStringAsync().Result;
这个post的签名吃了post数据:
public HttpResponseMessage Post([FromBody]string postdata)
将其更改为:
public HttpResponseMessage Post()
然后这个调用工作正常,以获取发布数据:
string str = response.Content.ReadAsStringAsync().Result;
测试了我自己。 使用第一个签名,str为空,使用第二个str有post数据!
我意识到这已经过时了,并且已经得到了解答,但是对于它的价值,你不能使用ReadAsStringAsync()
原因并不是因为它“按照建议的那样”来处理数据,而是因为内容正在被处理为一个流,并且由于消息格式化器已经消耗了数据,因此流的位置已经在最后。
要使用ReadAsStringAsync()
,首先需要将内容流Position重置为开头。
我这样做: response.RequestMessage.Content.ReadAsStreamAsync().Result.Seek( 0, System.IO.SeekOrigin.Begin )
因为我只有HttpResponseMessage,但是你可以直接访问HttpRequestMessage(就像你做的那样)在Controller内部你可以使用Request.Content.ReadAsStreamAsync().Result.Seek( 0, System.IO.SeekOrigin.Begin )
,我认为它在function上是等价的。
我相信你对ApiController吃Request.Content是正确的。 您在ApiController中看到的“Request”对象实际上是System.Net.Http.HttpRequestMessage类型。 我能够解决这个问题,但备份到System.Web.HttpRequest对象,如下所示:
Dim content as string If HttpContext.Current.Request.InputStream.CanSeek Then HttpContext.Current.Request.InputStream.Seek(0, IO.SeekOrigin.Begin) End If Using reader As New System.IO.StreamReader(HttpContext.Current.Request.InputStream) content = reader.ReadToEnd() End Using
我不知道寻求倒带是否有必要,但我把它放在以防万一。
我最后通过inheritance基本接口而不是ApiController来实现这一点 – 我认为ApiController是模型绑定,它正在吃响应
编辑:构建代理的正确方法是MessageHandler,而不是ApiController
- Request.Content.ReadAsStreamAsync()。Result.Seek(0,System.IO.SeekOrigin.Begin)
- new System.IO.StreamReader(Request.Content.ReadAsStreamAsync()。Result).ReadToEnd()
尝试使用ReadAsAsync
替换ReadAsStringAsync()
ReadAsAsync
。
您应该为您的参数使用复杂类型,然后在正文中使用一些json之类的
{路径:“c:…”}
Als使用了
Content-Type:application / json; 字符集= UTF-8
你的post请求中的标题,以便web api知道json包含在正文中
上述就是C#学习教程:无法在ASP.NET WebApi控制器中读取Request.Content分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/cdevelopment/1031400.html