1

I have troubles with my 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.

My Asp .net core Controller:

[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:

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:

Calling the route from my Angular Client

Calling the route from Postman:

Calling the route from Postman

Output of the asp .net core server console:

Output of the asp .net core server console

As you can see in the console logs from the server there is a 404 and a 200 Request finished. The one with 404 is my Application the one with 200 is the one from Postman.

Note:

CORS shouldn't be a problem since I have the google chrome extension.

I also tried to avoid this error with proxy configuration which angular supports.

miselking
  • 3,043
  • 4
  • 28
  • 39
  • you can correct the chaining first like 'this.http' needs to be chained not 'this'. – alokstar Oct 17 '18 at 18:40
  • Sorry my bad. It was just bad editing fixed it now. – Matthias Herzog Oct 17 '18 at 18:42
  • you don't need to put http://localhost:4000 in your URL. – alokstar Oct 17 '18 at 18:46
  • the "It worked" string but it doesn't even go that far because the Application doesn't find the route. – Matthias Herzog Oct 17 '18 at 18:47
  • What are the `Headers` you send through _Postman_? I see there are 4, maybe you need some (or all) of them to be sent from _Angular_ app as well... – miselking Oct 17 '18 at 18:55
  • this can help! https://stackoverflow.com/questions/52047548/response-for-preflight-does-not-have-http-ok-status-in-angular – alokstar Oct 17 '18 at 18:55
  • @miselking the headers are only the content type, a date, the server and transfer-encoding. I set the content type in my Angular Application and I don't think i need the others. – Matthias Herzog Oct 17 '18 at 18:59
  • @alokstar thanks I also saw that question and tried the proxy configuration part, because there is a CORS extension for chrome which I use and which normally works but sadly no success. – Matthias Herzog Oct 17 '18 at 19:01

1 Answers1

1

As you can see you got a 404 for OPTIONS request. We send options request when we send crossdomain request.

Please ensure that your CORS in asp.net app is configured. Because when you use POSTMAN it's not a crossdomain request.

You should add response header Access-Control-Allow-Origin: *

Alex Kvitchastyi
  • 250
  • 2
  • 11