繁体   English   中英

IIS 上的 Angular Post 到 Asp.Net(非核心)Web Api 错误 415

[英]Angular Post to Asp.Net (not Core) Web Api Error 415 on IIS

我在调用一个简单的 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; }
}

角度邮政编码:

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);
    }
}

再说一次,我永远不会明白为什么事情不应该如此复杂(有时甚至是矛盾的)。

我能够在自托管 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'
    })
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM