简体   繁体   English

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

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

I'm going completely nuts in making a simple Web Api call to work and I'm quite frustrated because things are much more complicated than they should be.我在调用一个简单的 Web Api 来工作时完全疯了,我很沮丧,因为事情比他们应该的要复杂得多。

I created an extremely simple Web Api (for testing) that is consumed by an Angular 6 client and I was able to make it to work if I self-host it locally but if I publish it to my Win10 local IIS (that is the way it will work when deployed to a server) then the request to Web Api fails with error 415 "Unsupported Media Type".我创建了一个非常简单的 Web Api(用于测试),由 Angular 6 客户端使用,如果我在本地自托管它,我就可以让它工作,但是如果我将它发布到我的 Win10 本地 IIS(就是这样)它会在部署到服务器时工作)然后对 Web Api 的请求失败并显示错误 415“不支持的媒体类型”。

The weird thing is that if I make the request to the self-hosted Web Api (which works) browser network tab is quite different than requesting to IIS published version.奇怪的是,如果我向自托管的 Web Api(有效)浏览器网络选项卡发出请求与请求 IIS 发布版本完全不同。

This is my Web Api method:这是我的 Web Api 方法:

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

I have to mention that it took me a whole morning to make it to work even self-hosted because of CORS and the key was adding the [HttpOptions] in method header.我不得不提一下,由于 CORS 的原因,我花了一整个上午才让它工作,甚至是自托管,关键是在方法标头中添加 [HttpOptions]。

Class Credentials:班级证书:

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

Angular post code:角度邮政编码:

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) ...

Network tab info when self-hosted (working one):自托管时的网络选项卡信息(工作):

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

Network tab info when published to local IIS (NOT working one):发布到本地 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

So, as you see, when the Web Api is published to IIS the output in network tab is different and the header is not arriving.因此,如您所见,当 Web Api 发布到 IIS 时,网络选项卡中的输出不同并且标头未到达。

I'm completely stuck and frustrated my friends.我完全被困住了,让我的朋友们感到沮丧。 Please help.请帮忙。

Edit 1: I add my WebApiConfig where you can see I enable cors just in case.编辑 1:我添加了我的 WebApiConfig,您可以在其中看到我启用了 cors 以防万一。

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

Again, I'll never understand why things are so complicated (and sometimes contradictory) when they shouldn't.再说一次,我永远不会明白为什么事情不应该如此复杂(有时甚至是矛盾的)。

The way I was able to make the request successfully in self-hosted Web Api as well as published in IIS Web Api was to replace application/json by application/x-www-form-urlencoded but why?我能够在自托管 Web Api 中成功发出请求以及在 IIS Web Api 中发布请求的方法是将 application/json 替换为 application/x-www-form-urlencoded 但为什么呢? This is a contradiction as I'm clearly sending json data.这是一个矛盾,因为我显然在发送 json 数据。

Anyway, not it works so I'll mark my own question as resolved.无论如何,它不起作用,所以我会将我自己的问题标记为已解决。

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

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

相关问题 Web API ASP.NET 核心发布请求 415 不支持的媒体类型 - Web API ASP.NET Core Post Request 415 Unsupported Media Type 如何将图像文件发布到 asp.net core 5 中的 api 控制器? (415 错误) - how do you post an image file to an api controller in asp.net core 5? (415 error) Angular POST从ASP.NET Core Web API返回500错误 - Angular POST returns 500 error from ASP.NET Core Web API 尝试将文件从 Angular 6 发布到 ASP.NET Core Web Api 时出错 - Error on try to post file from Angular 6 to ASP.NET Core Web Api ASP.NET Core 5 Web API,POST 返回 CreatedAtAction 错误 - ASP.NET Core 5 Web API, POST return CreatedAtAction error POST到内容类型为x-www-form-urlencoded的asp.net Web API会引发错误415不支持的媒体类型 - POST to asp.net web api with content type x-www-form-urlencoded raises error 415 unsupported media type ASP.NET Core 2.0 Web API IIS 托管和调试 - ASP.NET Core 2.0 Web API IIS Hosting and Debugging ASP.NET 最小内核 6 web API 部署到 Z5DA5ACF461B4EFB7E26EC86106Z5B21 - ASP.NET Core 6 Minimal web API Deploy To IIS 从Angular HttpClient到ASP.NET Core 2.2 Web API进行POST调用时出现404错误(启用了CORS) - 404 error when making a POST call from Angular HttpClient to ASP.NET Core 2.2 Web API (with CORS enabled) 将文件从 ASP.NET Core web api 发布到另一个 ASP.NET Core web api - Post files from ASP.NET Core web api to another ASP.NET Core web api
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM