NET的WebAPI控制器读取Request

NET的WebAPI控制器读取Request

本文介绍了不能在ASP.NET的WebAPI控制器读取Request.Content的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写在使用的WebAPI的代理TransferMode.Streamed HttpSelfHostConfiguration exe文件。

I am writing a proxy using WebApi in a TransferMode.Streamed HttpSelfHostConfiguration exe.

当我使用Fiddler张贴到我ApiController,由于某种原因,我无法读取Request.Content - 返回即使我已经发布的数据

When I use fiddler to post to my ApiController, for some reason I cannot read the Request.Content - it returns "" even if I have POSTed data

public class ApiProxyController : ApiController
{

    public Task<HttpResponseMessage> 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<HttpResponseMessage> Proxy(string path, HttpContent content)
    {
        ...
    }
}

下面是我的web请求

POST http://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?

What I am doing wrong? Why is s.Result coming back as the empty string rather than the raw json?

推荐答案

我得到这个由底座接口,而不是继承ApiController在结束工作 - 我觉得ApiController是modelbinding这是吃了回应

I got this working in the end by inheriting from the base interface instead of ApiController - I think the ApiController was modelbinding which was eating the response

编辑:建立一个代理权的事情是的MessageHandler,而不是一个ApiController

edit: The right thing for building a proxy is a MessageHandler, not an ApiController

这篇关于不能在ASP.NET的WebAPI控制器读取Request.Content的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 07:34