Skip to content

CORS AngularJS Api Controller Error 415  #19539

@LucasArgate

Description

@LucasArgate

Im using .net core (vNext) application with Kestrel.
When i use GET method it works perfectly request, but when i use POST or PUT method it not do complete request.

Im using Api Controller and i created an attribute filter to do pre-flight in OnActionExecuting to return status code 200. When i do POST method it not works.

see my OnActionExecuting method:

public override void OnActionExecuting(ActionExecutingContext filterContext) 
{
    HttpContext context = filterContext.HttpContext;
    context.Response.Headers.Add("Access-Control-Allow-Origin", "*");
    context.Response.Headers.Add("Access-Control-Allow-Headers", "token, content-type");
    context.Response.Headers.Add("Access-Control-Allow-Methods", "GET, POST, OPTIONS");

    if (context.Request.Method == "OPTIONS")
    {
        filterContext.Result = new StatusCodeResult(200);
        return;
    }
}

im trying to enable CORS in my startup class:

public void ConfigureServices(IServiceCollection services)
{
    // Add service and create Policy with options
    services.AddCors(options =>{
        options.AddPolicy("CorsPolicy",
            builder => builder.AllowAnyOrigin()
                .AllowAnyMethod()
                .AllowAnyHeader()
                .AllowCredentials());
        });
    // Add framework services.
    services.AddMvc();
}

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
    loggerFactory.AddConsole(Configuration.GetSection("Logging"));
    loggerFactory.AddDebug();

    app.UseMvc();
    app.UseCors("CorsPolicy");
}

Error request:

XMLHttpRequest cannot load http://192.168.100.100:5000/api/caixa. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://127.0.0.1:8080' is therefore not allowed access. The response had HTTP status code 500.

I don't know what i need do to resolve it, i search it and there isn't any solution. Please, I need help.

*Obs: I will use it in WIndow 10 IoT at Raspberry PI3.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions