简体   繁体   English

c# asp .net core HTTP POST 请求适用于 Postman,但不适用于我的 Angular 客户端(404 错误)

[英]c# asp .net core HTTP POST Request works with Postman but not with my angular client(404 error)

I have troubles with my c# asp .net core Rest Server.我的 c# asp .net core Rest Server 有问题。 I created an AuthenticationController with a test Route which seems to be working with Postman but when I am trying to get the same result in my Angular 6 Application I get an 404 error that the route is not found, although it is the exact same url.我创建了一个带有测试路由的 AuthenticationController ,它似乎正在与 Postman 一起工作,但是当我尝试在我的 Angular 6 应用程序中获得相同的结果时,我收到一个 404 错误,即找不到路由,尽管它是完全相同的 url。

My Asp .net core Controller:我的 Asp .net 核心控制器:

[Authorize]
[ApiController]
[Route("api/[controller]")]
public class AuthenticationController : ControllerBase
{
    private readonly IAuthenticationService _service;

    public AuthenticationController(IAuthenticationService service)
    {
        _service = service;
    }


    [AllowAnonymous]
    [HttpPost("[action]")]
    public IActionResult Test()
    {
        return Ok("It worked");
    }

}

My Angular Service where the route is called:我调用路由的 Angular 服务:

import {Injectable} from '@angular/core';
import {HttpClient, HttpErrorResponse, 
HttpHeaders}from'@angular/common/http';


const SERVER_URL = 'http://localhost:4000/api/authentication/';

@Injectable({providedIn: 'root'})
export class LoginService {
public token: string;

constructor(private sessionUser: SessionUserService, private http: 
HttpClient, private rolesService: RolesService) {}



test() {
  return this.http.post(SERVER_URL + 'test', {})
  .map((response: any) => {
    console.log(response);
    return response;
  })
  .catch(err => {
    console.log(err);
    return Observable.of(false);
  });
 }

}

and here are the different results:以下是不同的结果:

Calling the route from my Angular Client:从我的 Angular 客户端调用路由:

从我的 Angular 客户端调用路由

Calling the route from Postman:从 Postman 调用路由:

从 Postman 调用路由

Output of the asp .net core server console: asp .net 核心服务器控制台的输出:

asp .net 核心服务器控制台的输出

As you can see in the console logs from the server there is a 404 and a 200 Request finished.正如您在来自服务器的控制台日志中看到的那样,有一个 404 和一个 200 请求已完成。 The one with 404 is my Application the one with 200 is the one from Postman.带有 404 的是我的应用程序,带有 200 的是来自 Postman 的应用程序。

Note:笔记:

CORS shouldn't be a problem since I have the google chrome extension. CORS 应该不是问题,因为我有 google chrome 扩展。

I also tried to avoid this error with proxy configuration which angular supports.我还尝试使用 angular 支持的代理配置来避免此错误。

As you can see you got a 404 for OPTIONS request.如您所见,您收到了 OPTIONS 请求的 404。 We send options request when we send crossdomain request.当我们发送跨域请求时,我们发送选项请求。

Please ensure that your CORS in asp.net app is configured.请确保您在 asp.net 应用程序中的 CORS 已配置。 Because when you use POSTMAN it's not a crossdomain request.因为当您使用 POSTMAN 时,它不是跨域请求。

You should add response header Access-Control-Allow-Origin: *您应该添加响应头 Access-Control-Allow-Origin: *

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM