f#允许cors web api
我目前正在使用f#开发一个web api,我对它完全陌生,因为我来自c#背景,我想从reactjs应用程序获取数据。 但是我需要在我的f#webapi上允许cors。当我试图允许cors时,我完全迷失了方向。 到目前为止,我补充说:f#允许cors web api,f#,cors,c#-to-f#,F#,Cors,C# To F#,我目前正在使用f#开发一个web api,我对它完全陌生,因为我来自c#背景,我想从reactjs应用程序获取数据。 但是我需要在我的f#webapi上允许cors。当我试图允许cors时,我完全迷失了方向。 到目前为止,我补充说: member this.ConfigureServices(services: IServiceCollection) = services.AddCors() |> ignore 我试图补充一点 app.UseCors() |> ignore
member this.ConfigureServices(services: IServiceCollection) =
services.AddCors() |> ignore
我试图补充一点
app.UseCors() |> ignore
[<EnableCors("...")>]
配置成员,但我不知道如何实现此方法以允许在我的应用程序中使用cors。
我还试图补充一点
app.UseCors() |> ignore
[<EnableCors("...")>]
根据,我怀疑您应该在UseMvc
之前调用UseCors
中间件:
app.UseCors(Action<CorsPolicyBuilder> ConfigureCors)
.UseMvc()
.UseHttpsRedirection()
如果使用
curl运行curl客户机http://localhost:3000 -v
您应该能够检查响应中的CORS标题。您使用的是什么库?这看起来像长颈鹿,但它是长颈鹿还是其他什么?我没有使用任何库,只是使用defaut asp net f#项目谢谢你的回答,我回家后会尝试一下,但是如果你尝试从中访问值,它是否有效?我真的不知道如何将其应用到visual Studio生成的默认web api项目中。请看一下MSDN文档中的内容。您只需在返回HttpListenerResponse
之前添加CORS头。您可以在您想要的任何端口上运行服务器(coures的保留端口除外)。
module Server
open System
open System.Net
type HttpHandler = (HttpListenerRequest -> HttpListenerResponse -> Async<unit>)
type HttpListener with
static member Run (url: string, handler: HttpHandler) =
let listener = new HttpListener ()
listener.Prefixes.Add url
listener.Start ()
let asynctask = Async.FromBeginEnd(listener.BeginGetContext, listener.EndGetContext)
async {
while true do
let! ctx = asynctask
// Add the headers here
ctx.Response.AddHeader ("Access-Control-Allow-Origin", "*")
Async.Start (handler ctx.Request ctx.Response)
} |> Async.Start
listener
let run port =
HttpListener.Run ("http://localhost" + port + "/", fun req res ->
async {
let out = Text.Encoding.ASCII.GetBytes "Hello, Mars!"
res.OutputStream.Write (out, 0, out.Length)
res.OutputStream.Close()
}
) |> ignore
printfn "Running server on localhost:%s" port
Console.Read () |> ignore
// Main.fs
open Server
[<EntryPoint>]
let main argv =
Server.run ":3000"
0