c# - IIS 上的 Angular 发布到 Asp.Net(非核心)Web Api 错误 415
问题描述
我在做一个简单的 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/x-www-form-urlencoded 替换 application/json 但为什么呢?这是一个矛盾,因为我显然在发送 json 数据。
无论如何,它不起作用,所以我将我自己的问题标记为已解决。
let headers={
headers: new HttpHeaders({
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
})
}
推荐阅读
- loops - FFMPEG vstack 和循环
- angular - 从网站打开本地电脑上的应用程序 - 角度/打字稿
- react-router - 包装组件时反应路由器显示不同
- javascript - 单击操作不适用于通知 pwa
- java - saveAll() is too slow. Cassandra Database with Spring boot. why?
- android - 我的 ionic apk 没有连接到网络服务器
- javascript - 通过 mongoose 查询 mongoDB 时出现问题
- c# - 在动态编译器中引用由 NuGet 导入的程序集
- azure - Azure 机器人与经过身份验证的消息传递端点交互
- javascript - React Hooks:为什么将设置状态函数传递给子组件是不好的做法?