从HttpResponseMessage读取HttpContent

时间:2017-12-25 19:23:32

标签: .net asp.net-web-api asp.net-web-api2 httpresponsemessage

我从My API控制器发送HttpResponseMessage以下内容

public HttpResponseMessage Upload()
{
    HttpResponseMessage response = new HttpResponseMessage();
    HttpRequestMessage request = new HttpRequestMessage();
    if (System.Web.HttpContext.Current.Request.Files.Count > 0)
    {
        var file = System.Web.HttpContext.Current.Request.Files[0];
        var path = System.Web.Hosting.HostingEnvironment.MapPath("----------");
        bool folderExists = Directory.Exists(path);
        if (!folderExists)
            Directory.CreateDirectory(path);

        string pathWithFileName = Path.Combine(path, file.FileName);
        file.SaveAs(pathWithFileName);

        response.StatusCode = HttpStatusCode.Created;
        response.Content = new StringContent(pathWithFileName, System.Text.Encoding.UTF8, "application/json");

        response.Content = new JsonContent(new
        {
            Name = "a",
            Address = "b",
            Message = "Any Message" 
        });   

        return response;
    }
    else
    {
        response.StatusCode = HttpStatusCode.BadRequest;
        return response;
    }
}

我正在尝试阅读以下内容

1 - 创建了一个扩展方法

public static string ContentToString(this HttpContent httpContent)
{
    var readAsStringAsync = httpContent.ReadAsStringAsync();
    return readAsStringAsync.Result;
}

阅读内容如下

var av = result.Content.ContentToString();

输出

{"result":null,"targetUrl":null,"success":true,"error":null,"unAuthorizedRequest":false,"__abp":true}

为什么我没有收到内容? 请指教。

0 个答案:

没有答案