1

我正在从我的客户端(ASP.NET MVC)向 webapi 服务发送发布请求,但是,webapi 服务控制器需要一个Stream始终为null.

来自 ASP.NET MVC 应用程序的请求:

using(var client = new HttpClient())
{
    Token token = GetToken();
    client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(token.TokenType, token.AccessToken);
    var content = new StreamContent(stream);
    content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
    content.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data") { Name = "keliaStream" };
    var res = client.PostAsync(GetSdcUri(), content).Result;
}

WebApi 控制器:

// POST: api/Publication
[HttpPost]
[Route("whatever-route")]
[RequireHttps]
public System.Web.Mvc.ActionResult GenerateSDCFromTBox([FromBody]Stream keliaStream)
{
    // whatever
}

keliaStream始终为空,我错过了什么?

4

1 回答 1

0

感谢您的评论,我已经找到了解决方案。
如果有人遇到同样的问题,这是我的解决方案:

客户端(ASP.NET MVC):

using(var client = new HttpClient())
{
    Token token = GetToken();
    client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(token.TokenType, token.AccessToken);
    var content = new StreamContent(stream);
    content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
    var res = client.PostAsync(GetSdcUri(), content).Result;
}

服务器(WebApi):

public System.Web.Mvc.ActionResult GenerateSDCFromTBox()
{
    Stream stream = Request.Content.ReadAsStreamAsync().Result;
    // return do what ever with stream
}
于 2019-12-31T16:21:58.313 回答