简体   繁体   English

Angular POST从ASP.NET Core Web API返回500错误

[英]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.

相关问题 ASP.NET 5 Web API CreatedAtAction:POST 返回 500 - ASP.NET 5 Web API CreatedAtAction: POST returns 500 尝试将文件从 Angular 6 发布到 ASP.NET Core Web Api 时出错 - Error on try to post file from Angular 6 to ASP.NET Core Web Api 为什么Asp.net核心Web API 2.0返回Http Error 500 - why does Asp.net core Web API 2.0 returns Http Error 500 ASP.NET Core 2.0 Web API 抛出 500 错误并且不允许访问 http 帖子 - ASP.NET Core 2.0 Web API throwing 500 error and not allowing access on http post Asp.net Core Web API与MongoDB 500错误 - Asp.net core web api with mongodb 500 error ASP.NET Core Web API 500内部服务器错误 - ASP.NET Core Web API 500 Internal Server Error ASP.NET 内核 2.2 Web API ZC31C335EF37283C451B18BA0DD317DE1。 托管服务提供商说 500 - 内部服务器错误 - ASP.NET Core 2.2 Web API Angular. Hosting provider says 500 - Internal server error 将文件从 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 从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) IIS 上的 Angular Post 到 Asp.Net(非核心)Web Api 错误 415 - Angular Post to Asp.Net (not Core) Web Api Error 415 on IIS
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM