[英]Angular POST returns 500 error from ASP.NET Core Web API
I'm posting data
from Angular client to ASP.NET Core Web API. 我正在将
data
从Angular客户端发布到ASP.NET Core Web API。 In case when data
has latin europe chars like ć,č,š it returns 500 Internal error, otherwise it return 201. 如果
data
包含ć,č,š之类的拉丁字符,则返回500 Internal error,否则返回201。
This is data
class: 这是
data
类:
export class FinalFormData {
name: string;
url: string;
dateCreated: Date;
tags: any[];
}
Function for handling data for POST function: 用于处理POST函数数据的函数:
this.data.postData(this.finalData)
.subscribe(data => {
this.finalData = data;
this.router.navigate(["/"]);
}, err => console.log(err));
Function for posting data: 发布数据的功能:
public postData(data: FinalFormData): Observable<FinalFormData> {
return this.http.post<FinalFormData>("/api/links", data, {
headers: new HttpHeaders({
'Content-Type' : 'application/json; charset=utf-8'
})
});
}
Web API 网络API
[HttpPost("")]
public async Task<IActionResult> Post([FromBody]FinalFormViewModel theLink)
{
if (ModelState.IsValid)
{
var newLink = new Links
{
UserName = User.Identity.Name,
Url = theLink.Url,
Name = theLink.Name,
DateCreated = DateTime.UtcNow
};
//newLink.Name = _repository.GetTitle(newLink.Url);
_repository.AddLink(newLink);
_context.SaveChanges();
Links link = _repository.GetLinkByName(newLink.Name);
foreach (var tag in theLink.Tags)
{
var newTag = new Tags
{
Name = tag
};
_repository.AddTag(link, newTag, newLink.UserName);
}
// Returning data
var data = new FinalFormViewModel()
{
Name = newLink.Name,
Url = newLink.Url,
DateCreated = newLink.DateCreated,
Tags = newLink.Tags.Select(t => t.Name).ToList()
};
if (await _repository.SaveChangesAsync())
{
return Created($"api/links/{theLink.Name}", data);
}
}
return BadRequest("Failed to save the link");
}
When debugging in Web API in every case code reach return Created($"api/links/{theLink.Name}", data);
在每种情况下,在Web API中进行调试时,代码都将
return Created($"api/links/{theLink.Name}", data);
which means it returns 201 status. 这意味着它将返回201状态。
But in case when those chars like č,ć,ž are part of url
string even though it returs 201, in browser I get the error in console with message: 但是,即使č,ć,ž之类的字符即使返回201也属于
url
字符串,但在浏览器中我却在控制台中看到以下错误消息:
"Http failure response for http://localhost:54105/api/links: 500 Internal Server Error"
After this subscribing to an Observable won't reach success block. 订阅Observable之后将不会成功。 In other cases without those special chars it works fine.
在没有那些特殊字符的其他情况下,它可以正常工作。
Network response: 网络响应:
EDIT 编辑
I actually now find the server error: 我现在实际上发现服务器错误:
InvalidOperationException: Invalid non-ASCII or control character in header: 0x0161
Microsoft.AspNetCore.Server.Kestrel.Internal.Http.FrameHeaders.ThrowInvalidHeaderCharacter(char ch)
Angular can only support a single language at a time, so if u wanna switch from English to Latin, you need to reload the application context.. Angular一次只能支持一种语言,因此,如果您想从英语切换到拉丁语,则需要重新加载应用程序上下文。
More details here Angular 5 internationalization 更多详细信息请参见Angular 5国际化
jdweng is right. jdweng是正确的。 UTF-8 does not include the characters you mention : http://www.utf8-chartable.de/
UTF-8不包含您提到的字符: http : //www.utf8-chartable.de/
ISO-8859-2, on the other hand, includes them : https://www.terena.org/activities/multiling/ml-docs/iso-8859.html 另一方面,ISO-8859-2包括它们: https : //www.terena.org/activities/multiling/ml-docs/iso-8859.html
So as said here : https://stackoverflow.com/a/43164489/4770754 所以就像这里所说的: https : //stackoverflow.com/a/43164489/4770754
Include the RequestOptionsArgs when you send request.
发送请求时包括RequestOptionsArgs。
Specify the field responseType : ResponseContentType inside RequestOptionsArgs.
在RequestOptionsArgs中指定字段responseType:ResponseContentType。 (Either ResponseContentType.ArrayBuffer or ResponseContentType.Blob)
(ResponseContentType.ArrayBuffer或ResponseContentType.Blob)
Use TextDecoder or something similar to decode the result.
使用TextDecoder或类似方法解码结果。
See the docs:
参见文档:
https://angular.io/api/http/Http
https://angular.io/api/http/Http
https://angular.io/api/http/RequestOptions
https://angular.io/api/http/RequestOptions
https://angular.io/api/http/ResponseContentType
https://angular.io/api/http/ResponseContentType
UPDATE : 更新:
or you could do as you said and instead of theLink.Name
use theLink.Url
which won't contains non-ASCII chars. 或者您可以按照您所说的去做,而
theLink.Name
使用theLink.Url
而不是theLink.Url
,它不会包含非ASCII字符。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.