[英]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:但最佳做法是执行以下步骤:
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.