F# web api cors


#1

Hello,

I am new to f# and I am trying to create a web api to fetch data from my reactjs app.
But I am facing a problem with cors, I have tried to enablecors in my application but so far i couldn’t get it worked.
I have looked over internet, but there are only solution for Giraffe and Suave. I am developing the web api with the default project on visual studio.
So far here is what I have in my startup.fs :slight_smile:

module ConfigurationCors =
   let ConfigureCors(corsBuilder: CorsPolicyBuilder): unit =        
         corsBuilder.AllowAnyOrigin()
                        .AllowAnyHeader()
                        .AllowAnyMethod()
                        .AllowCredentials() |> ignore
open ConfigurationCors


type Startup private () =
new (configuration: IConfiguration) as this =
    Startup() then
    this.Configuration <- configuration 

// This method gets called by the runtime. Use this method to add services to the container.
member this.ConfigureServices(services: IServiceCollection) =
// Add framework services.
services.AddCors() |> ignore


services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1) |> ignore


// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
member this.Configure(app: IApplicationBuilder, env: IHostingEnvironment) =
if (env.IsDevelopment()) then
    app.UseDeveloperExceptionPage() |> ignore
else
    app.UseHsts() |> ignore
app.UseHttpsRedirection() |> ignore
app.UseMvc() |> ignore
app.UseCors(Action<CorsPolicyBuilder> ConfigureCors) |> ignore

member val Configuration : IConfiguration = null with get, set

I am stuck because I don’t really know how to enable cors, I ``` tried to do the same thing you should do in a c# web api but it’s not working.
If someone could help me.


#2

You should call app.UseCors… before app.UseMvc…
This is not related to F#, just asp.net core middleware stuff.