【问题标题】:Angular Post to Asp.Net (not Core) Web Api Error 415 on IISIIS 上的 Angular 发布到 Asp.Net(非核心)Web Api 错误 415
【发布时间】:2019-02-05 04:53:51
【问题描述】:

我在做一个简单的 Web Api 调用工作时完全发疯了,我很沮丧,因为事情比他们应该的要复杂得多。

我创建了一个非常简单的 Web Api(用于测试),由 Angular 6 客户端使用,如果我在本地自行托管它,但如果我将它发布到我的 Win10 本地 IIS(即是部署到服务器时的工作方式)然后对 Web Api 的请求失败并出现错误 415“不支持的媒体类型”。

奇怪的是,如果我向自托管 Web Api(有效)发出请求,浏览器网络选项卡与向 IIS 发布版本请求完全不同。

这是我的 Web Api 方法:

[HttpPost]
[HttpOptions]
public void Post([FromBody]Credentials cred)
{
    string strTest = "I'm doing just nothing";
}

我不得不提一下,由于 CORS,我花了一整个上午的时间才让它工作,甚至是自托管,关键是在方法头中添加 [HttpOptions]。

类凭证:

public class Credentials {
    public string Username { get; set; }
    public string Password { get; set; }
}

Angular 邮政编码:

let headers={
    headers: new HttpHeaders({
        'Accept': 'application/json',
        'Content-Type': 'application/json; charset=UTF-8'
    })
}

return this.http.post<any>('http://localhost:9810/api/Login', JSON.stringify({username, password}), headers) ...

自托管时的网络标签信息(工作之一):

General:
Request URL: http://localhost:9000/api/Login
Request Method: OPTIONS
Status Code: 204 No Content
Remote Address: [::1]:9000
Referrer Policy: no-referrer-when-downgrade

Response Headers:
Content-Length: 0
Date: Thu, 30 Aug 2018 09:24:50 GMT
Server: Microsoft-HTTPAPI/2.0

Request Headers:
Accept: */*
Accept-Encoding: gzip, deflate, br
Accept-Language: es-ES,es;q=0.9,en;q=0.8
Access-Control-Request-Headers: content-type
Access-Control-Request-Method: POST
Connection: keep-alive
Host: localhost:9000
Origin: http://localhost:4200
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36

发布到本地 IIS 时的网络选项卡信息(不起作用):

General:
Request URL: http://localhost:9810/api/Login
Request Method: OPTIONS
Status Code: 415 Unsupported Media Type
Remote Address: [::1]:9810
Referrer Policy: no-referrer-when-downgrade

Response Headers:
Content-Length: 801
Content-Type: application/json; charset=utf-8
Date: Thu, 30 Aug 2018 08:57:58 GMT
Server: Microsoft-IIS/10.0
X-Powered-By: ASP.NET

Request Headers:
Accept: */*
Accept-Encoding: gzip, deflate, br
Accept-Language: es-ES,es;q=0.9,en;q=0.8
Access-Control-Request-Headers: content-type
Access-Control-Request-Method: POST
Connection: keep-alive
Host: localhost:9810
Origin: http://localhost:4200
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36

因此,如您所见,当 Web Api 发布到 IIS 时,网络选项卡中的输出不同,并且标头未到达。

我完全被我的朋友们困住并感到沮丧。请帮忙。

编辑 1:我添加了我的 WebApiConfig,您可以看到我启用了 cors,以防万一。

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        var cors = new EnableCorsAttribute("http://localhost:4200", "*", "*");
        config.EnableCors(cors);
    }
}

【问题讨论】:

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


    【解决方案1】:

    再说一次,我永远也不会明白为什么事情会如此复杂(有时甚至是矛盾的),而实际上它们本不该如此。

    我能够在自托管 Web Api 中成功发出请求以及在 IIS Web Api 中发布请求的方法是将 application/json 替换为 application/x-www-form-urlencoded 但为什么呢?这是一个矛盾,因为我显然在发送 json 数据。

    无论如何,它不起作用,所以我将我自己的问题标记为已解决。

    let headers={
        headers: new HttpHeaders({
            'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
        })
    }
    

    【讨论】:

    • this 帖子中的答案解释了它。 If you need to get multiple values from the request body, define a complex type.
    • 我不需要多个值,而是一个 json,这是当今最常见的场景和数据类型,但这并不是让我发疯的原因,但事实上我已经在使用“应用程序”进行请求/json" 标头,这样请求就会被拒绝,但是当标头的 Content-Type 为 "application/x-www-form-urlencoded" 时它可以工作。看起来很矛盾。
    猜你喜欢
    • 2021-04-27
    • 2017-06-16
    • 1970-01-01
    • 2020-03-24
    • 2018-08-08
    相关资源
    最近更新 更多