简体   繁体   English

415(不支持的媒体类型)将数据从 angular9 传递到 asp.net web api

[英]415(unsupported media type) when passing data from angular9 to asp.net web api

I am getting a 415 unsupported media type error when doing a get request with data to my backend API.在向我的后端 API 发送数据请求时,我收到 415 不支持的媒体类型错误。 when testing in postman it's working fine but in angular I cant seem to make it work.在 postman 中测试时,它工作正常,但在 angular 中我似乎无法使其工作。

angular angular

  onExitSelectionChanged(event: MatSelectChange) {
    this.getFare(this.selectedEntry, this.selectedExit);
  }

 getFare(A, B) {
    var stations = new ComputeFareDTO();
    stations.stationA = A;
    stations.stationB = B;

    this.getFareRequest(stations).subscribe(res => {
      this.Fare = res;
    })
  }

  getFareRequest(data: any): Observable<any> {
    return this.http.get<any>(this.baseUrl + 'api/card/getFare', data);
  }

class ComputeFareDTO implements IComputeFareDTO {
  stationA: string;
  stationB: string;
}

interface IComputeFareDTO {
  stationA: string;
  stationB: string;
}

C# C#

  [Route("getFare")]
    public decimal GetFareMrtTwo(ComputeFareDTO comp) 
    {
       return GetFare(comp.StationA, comp.StationB);
    }

 public class ComputeFareDTO
    {
        public string StationA { get; set; }
        public string StationB { get; set; }
    }
}

its getting an error它出现错误

GET localhost:xxxxx/api/card/getFare 415 (Unsupported MediaType) GET localhost:xxxxx/api/card/getFare 415(不支持的媒体类型)

unless otherwise specified, data is sent using Content-Type: application/x-www-form-urlencoded除非另有说明,否则使用Content-Type: application/x-www-form-urlencoded发送数据

To solve this issue, make sure that your ASP.NET CORE app accepts application/json by adding [FromBody] attribute before your dto declaration like public decimal GetFareMtrTwo([FromBody] ComputeFareDTO comp) and make sure that your angular client sends data as JSON To solve this issue, make sure that your ASP.NET CORE app accepts application/json by adding [FromBody] attribute before your dto declaration like public decimal GetFareMtrTwo([FromBody] ComputeFareDTO comp) and make sure that your angular client sends data as JSON

this type of sending the request has two errors:这种类型的发送请求有两个错误:

1- programmatic error, that if you are sending some data to server you should send your data in application/JSON body type. 1- 程序错误,如果您向服务器发送一些数据,您应该以应用程序/JSON 正文类型发送数据。

2- conceptual error: If you are fetching something from the server, and you need to send some searchable parameters, you should send them via query string or something like GraphQL. 2- 概念错误:如果您从服务器获取某些内容,并且需要发送一些可搜索的参数,您应该通过查询字符串或类似 GraphQL 的方式发送它们。 If you are sending some data to the server, you should send them through POST/PUT method.如果您要向服务器发送一些数据,您应该通过 POST/PUT 方法发送它们。

Anyway you can do something like this: (but its conceptually wrong, because GET requests should not have any BODY):无论如何,你可以这样做:(但它在概念上是错误的,因为 GET 请求不应该有任何 BODY):

[HttpPost("getFare")]
public decimal GetFareMrtTwo([FromBody] ComputeFareDTO comp) 
{
   return GetFare(comp.StationA, comp.StationB);
}

and you can send your post request like this .你可以像这样发送你的帖子请求。

NOTE: you should send your body with json format using JSON.stringify method.注意:您应该使用 JSON.stringify 方法以 json 格式发送您的身体。

The first and wrong solution is my words above:|.第一个也是错误的解决方案是我上面的话:|。 But best practice is doing bellow steps:但最佳做法是执行以下步骤:

  • Initiate your request with GET method and send parameters through query string使用 GET 方法发起请求并通过查询字符串发送参数
  • Write a simple ModelBinder that binds query params to your DTO model.编写一个简单的 ModelBinder,将查询参数绑定到您的 DTO model。

NOTE: there is no need to stringify your parameters as json in this solution <3.注意:在此解决方案 <3 中,无需将您的参数字符串化为 json。

NOTE: Here you can see how to write a simple model-binder.注意: 在这里你可以看到如何编写一个简单的模型绑定器。

Have fun.玩得开心。

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

相关问题 Web API ASP.NET 核心发布请求 415 不支持的媒体类型 - Web API ASP.NET Core Post Request 415 Unsupported Media Type 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 415 不支持的媒体类型 asp.net 核心 - 415 Unsupported Media Type asp.net core 错误 415:不支持的媒体类型 - ASP.NET MVC - Error 415: Unsupported Media Type - ASP.NET MVC ASP.NET 核心 WebAPI:不支持的媒体类型 - HTTP 415 - ASP.NET Core WebAPI: Unsupported Media Type - HTTP 415 远程服务器返回错误:(415)在asp.net中使用API​​的不支持的媒体类型 - The remote server returned an error: (415) Unsupported Media Type using API in asp.net 415到.NET Web API的不支持的媒体类型 - 415 Unsupported Media Type on POST to .NET Web API 将带有 JSON 数据的 GET 请求从 Axios 发送到 Asp.net 核心 ActionResult 而不获取(415 不支持的媒体类型) - Send GET request with JSON data from Axios to Asp.net core ActionResult without getting (415 Unsupported Media Type) Angular 7 和 .NET Core API 中不支持的媒体类型 415 错误 - Unsupported Media Type 415 Error in Angular 7 & .NET Core API .net核心Web API-415不支持的媒体类型 - .net core Web API - 415 Unsupported Media Type
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM