【问题标题】:ASP.NET Web API does not receive the data posted from AngularASP.NET Web API 不接收从 Angular 发布的数据
【发布时间】:2019-09-12 12:47:45
【问题描述】:

我目前正在尝试将 Angular 模型发布到 .NET Core MVC 中的 Web API。 Angular 端的模型在发布到 Web API 之前正确填充。然而,当它到达 API 时,模型看起来像 {"urlEndpoint":null,"token":null}

我尝试更改标题Content-Type,我尝试添加[FromBody],我还尝试将控制器从HttpResponseMessage 更改为IHttpActionResult - 实际上几乎所有关于堆栈溢出的解决方案都与类似问题有关。然而,有趣的是,同样的代码可以在标准 .NET 上的旧项目中使用。

对此的任何帮助将不胜感激......这让我发疯了!

角度组件:

 getPdfData() {

    let token = this.httpService.getToken("token");

    this.urlAndTokenModel = new UrlAndTokenModel();

    this.urlAndTokenModel.Token = token;
    this.urlAndTokenModel.UrlEndpoint = this.apiEndpoint;
    this.httpService.postPdfBytes('/blahblah/api/pleaseworkthistime', this.urlAndTokenModel, this.successCallback.bind(this),
        this.failureCallback.bind(this));

}

Angular http.service

    postPdfBytes(url: string, data: UrlAndTokenModel, successCallback, errorCallback) {

    return this.http.post(url, data,
        {
            headers: new HttpHeaders().set('Content-Type', 'application/json'),
            responseType: 'blob'
        }

    ).subscribe(

        resp => successCallback(resp),
        error => errorCallback(error)
        );
}

网络 API:

    public class TestController : BaseController
{
    public TestController(ICacheHelper cacheHelper) : 
        base(cacheHelper)
    {
    }

    [Route("api/pleaseworkthistime")]
    [HttpPost]
    public HttpResponseMessage GetDocument(UrlAndTokenModel data)
    {

        var client = new HttpClient();

        client.DefaultRequestHeaders.Add("Authorization", data.Token);

        var responseTask = client.GetAsync(data.UrlEndpoint);

        responseTask.Wait();

        var result = responseTask.Result;

        byte[] finalResult = null;

        if (result.IsSuccessStatusCode)
        {

            var readTask = result.Content.ReadAsByteArrayAsync();
            readTask.Wait();

            finalResult = readTask.Result;

        }

        var httpRequestMessage = new HttpRequestMessage();
        var httpResponseMessage = httpRequestMessage.CreateResponse(HttpStatusCode.OK);

        httpResponseMessage.Content = new ByteArrayContent(finalResult);
        httpResponseMessage.Content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment");
        httpResponseMessage.Content.Headers.ContentDisposition.FileName = "mytestpdf.pdf";
        httpResponseMessage.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/octet-stream");

        return httpResponseMessage;
    }

}

那么显然有角度 URLAndTokenModel 带有 URLEndpoint 和一个令牌 - 在 C# 模型中也是如此。

【问题讨论】:

  • 确定您的客户端中的模型可以反序列化为您的 C# 模型吗?如果没有,请将它们添加到您的问题中。
  • 100% :) 就像我说的,我在不同的项目中使用了相同的代码,这就是为什么会如此混乱。
  • 并且您已将 MVC 配置为使用 camelCase 属性名称?
  • 我确实有:)
  • 所有的外观(和声音)都在那个时候。你能把相关的部分提取出来发到github上吗?

标签: c# asp.net asp.net-mvc angular asp.net-web-api


【解决方案1】:

终于解决了!我希望这可以帮助其他人解决同样的问题。

post 到 .NET API 时,首先需要 JSON.Stringify

 this.httpService.postPdfBytes('/blahblah/api/pleaseworkthistime', JSON.stringify(this.urlAndTokenModel), this.successCallback.bind(this),
        this.failureCallback.bind(this));

angular http.service 因此也需要更新为字符串而不是模型。

postPdfBytes(url: string, data: string, successCallback, errorCallback)

此时它仍然没有它,然后将 [FromBody] 简单地添加到 Controller 和 walahh!

public HttpResponseMessage GetDocument([FromBody] UrlAndTokenModel data)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-29
    • 2016-08-09
    • 1970-01-01
    • 1970-01-01
    • 2017-09-27
    相关资源
    最近更新 更多