简体   繁体   中英

Failed http request WebApi using Angular

I am trying to connect a service created in .net from angular but I get an error with CORS, I have looked for many links but none have helped me since most of the solutions are for .NET CORE and I am using standard .net .

call from angular:

 return this.http.post('http://localhost:50112/api/auth', userdata).toPromise();

Api:

public class AuthController : ApiController
{
    public async Task<IHttpActionResult> Auth(User userdata)
    {
        var result = await bllUser.auth(userdata);
        return Ok(result);
    }
}

and this error:

Access to XMLHttpRequest at 'http://localhost:50461/api/auth' from origin 'http://localhost:4200' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.

I also tried adding the config.EnableCors statement in my webapiconfig file but it tells me that that statement does not exist

在此处输入图片说明

I would appreciate your help, no solution has served me

To use enable cors

Install CORS to the API project "Install-Package Microsoft. Asp. Net. WebApi. Cors". And change your register method

public static void Register(HttpConfiguration config)
{
    var cors = new EnableCorsAttribute("www.example.com", "*", "*");
    config.EnableCors(cors);
    // ...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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