简体   繁体   English

从角度6的asp.net核心读取响应状态码

[英]Read status code on response from asp.net core in angular 6

I have an API in Asp.net core 2 and I need catch the status code from the response, but on my response in angular only have my API result. 我在Asp.net core 2中有一个API,我需要从响应中捕获状态代码,但是在我的角度响应中只有我的API结果。

My method on API 我的API方法

[HttpPost]
    [Route("cadastrar-categoria")]
    public async Task<IActionResult> CadastrarCategoriaAsync([FromBody]Categoria item)
    {
        try
        {
            var sucesso = await _service.CadastroCategoria(item);
            if (sucesso == 1)
            {
                return Ok("Sucesso");
            }
            else
            {
                return BadRequest("Erro");
            }
        }
        catch (Exception ex)
        {
            return BadRequest(ex.Message);
        }
    }

And my Service in angular 而我的服务角度

CadastrarCategoria(nome: string) {
 let data = new CadastroCategoriaDto();
 data.Nome = nome;

 return this.req
   .post(environment.urls.cadastrarProduto, data)
   .subscribe((res: Response) => {
     console.log(res);
   });

} }

My response in the browser is only this. 我在浏览器中的回答仅仅是这样。 enter image description here 在此处输入图片说明

How I catch the status code from this response? 如何从此响应中捕获状态代码? example in return Ok() I need receive status code 200. 返回Ok()的示例我需要接收状态码200。

If you want full response in subscribe method then use {observe: 'response'} . 如果您希望在Subscribe方法中得到完整的响应,请使用{observe: 'response'}

Example: 例:

return this.req
   .post(environment.urls.cadastrarProduto, data,{observe: 'response'})
   .subscribe((res: HttpResponse) => {
     console.log(res.status);
   });

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

相关问题 将来自asp.net核心的blob数据读取到Angular 7 - Read blob data that comes from asp.net core to Angular 7 从 Azure 中托管的 Asp.net Core MVC Web App 获取“响应状态代码并不表示成功:502(坏网关)” - Getting "Response status code does not indicate success: 502 (Bad Gateway)" from Asp.net Core MVC Web App hosted in Azure 状态码 406 使用 ASP.NET 内核中的格式响应数据 Web API - Status code 406 using Format response data in ASP.NET Core Web API ASP.Net Core 中间件无法设置异常状态码,因为“响应已经开始” - ASP.Net Core middleware cannot set status code on exception because "response has already started" 使用 http 拦截器在 Angular 应用程序中读取 asp.net 核心 http 响应标头 - Read asp.net core http response header in angular app with http interceptor ASP.NET Core中自动化要求失败的错误状态代码 - Wrong status code for failed autorization requirement in ASP.NET Core ASP.NET Core 返回带有状态代码的 JSON - ASP.NET Core return JSON with status code (代码=已退出,状态=150)在 apache asp.net 内核上 - (code=exited, status=150) on apache asp.net core ASP.NET - 在控制器响应中设置状态代码 - ASP.NET - setting status code in controller response Asp.net Web Api 将响应状态代码设置为数字 - Asp.net Web Api set response status code to number
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM