0

I have w big problem with request from angular 5 (localhost:4200) to my .NET Core WebApi (localhost:55007). I think I tried everything and all lost..

Situation looks: login.service.ts

import { Injectable, Inject } from '@angular/core';
import {HttpModule, Http, URLSearchParams, Headers, RequestOptions} from 
'@angular/http';

@Injectable()
export class LoginService {
  constructor( public http: Http) {
}

login(email:string, password:string){
  this.http.post('http://localhost:55007/Login/Login',         
{email:"fooad@ds.pl",password:"loqweqko"}).subscribe(
    res => { 
      console.log(res);
    },
    (err) => {
      console.log(err);
    }
 );
 }
}

webApi method:

[Route("[controller]/[action]")]
public class LoginController : Controller
{
    [HttpPost]
    public object Login([FromBody] LoginDto model)
    {
        //something..
    }
}

Where LoginDto is:

public class LoginDto
{
    [Required]
    public string Email { get; set; }
    [Required]
    public string Password { get; set; }
}

Please tell me what I'm doing wrong.. I have also tried use json.stringify as an object in request. Nothing happends.. I'm always getting 404.. (using postman all works correcty, also GET method works).

---------------------------EDIT---------------------------

Ok, I found solution - the problem was with CORS, I made the same as : How to enable CORS in ASP.net Core WebAPI

and for me works. Thanks :)

BGeeK
  • 19
  • 4
  • Is the webapi request case sensitive for the expected properties in requested object? Did you try putting Email and Password with capital case in the Json request? – Hugo Noro Mar 31 '18 at 21:37
  • @HugoNoro It should not matter, I've tried using postman and this works. – BGeeK Mar 31 '18 at 21:39
  • OK just ruling out options :). Not 100% sure of all the quirks of web api. – Hugo Noro Mar 31 '18 at 21:40
  • Can it be a CORS issue? Since you are running in the same localhost the browser will initially do an OPTIONS request, called the preflight and then will try the actual request. You will not face that issue in postman because you are not doing a cross origin request. Is the server configured to accept CORS? – Hugo Noro Mar 31 '18 at 21:43
  • Are you seeing any CORS errors in the browser developer console? – Martin Mar 31 '18 at 21:43
  • Now I'm using [DisableCors] attribute and this also doesn't work.. In console I have information that angular is in development mode and "The SSL certificate used to load resources from https://ocdn.eu will be distrusted in M70. Once distrusted, users will be prevented from loading these resources." Whats more in some of my changes I didnt get 404 on request, but information that request was canceled, and as Initiator was zone.js. – BGeeK Mar 31 '18 at 21:51

1 Answers1

0

Simple enabling of Cors for allow any origin/method etc(Personal project): 1. nuget: Microsoft.AspNetCore.Cors

  1. In configure method, add this before useMvc: app.UseCors(o =>o.AllowAnyOrigin().AllowAnyMethod().AllowAnyMethod().AllowCredentials());

  2. in ConfigureServices, before AddMvc, Add this: services.AddCors();

Sri
  • 277
  • 2
  • 9