Asp.net web api 如何在长颈鹿中启用CORS?
我无法在Elm客户端发送请求的服务器上使用Giraffe框架成功执行Post操作 我在尝试测试http请求时收到以下消息: 信息:Microsoft.AspNetCore.Hosting.Internal.WebHost Microsoft.AspNetCore.Hosting.Internal.WebHost:信息:请求 正在启动HTTP/1.1选项0 dbug: Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware 服务实现如下所示:Asp.net web api 如何在长颈鹿中启用CORS?,asp.net-web-api,f#,asp.net-core-webapi,f#-giraffe,Asp.net Web Api,F#,Asp.net Core Webapi,F# Giraffe,我无法在Elm客户端发送请求的服务器上使用Giraffe框架成功执行Post操作 我在尝试测试http请求时收到以下消息: 信息:Microsoft.AspNetCore.Hosting.Internal.WebHost Microsoft.AspNetCore.Hosting.Internal.WebHost:信息:请求 正在启动HTTP/1.1选项0 dbug: Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware 服务实现如下所示: l
let private registrationHandler =
fun(context: HttpContext) ->
async {
let! data = context.BindJson<RegistrationRequest>()
match register data with
| Success profile -> return! json profile context
| Failure -> return! (setStatusCode 400 >=> json "registration failed") context
}
附录:
POST >=>
choose [
route "/register" >=> registrationHandler
]
可以找到源文件
下面是一个示例,显示了支持Cors的代码
let configureApp(应用程序:IApplicationBuilder)=
app.UseGiraffeErrorHandler错误处理程序
app.UseStaticFiles()|>忽略
app.UseAuthentication()|>忽略
app.UseCors(新操作(乐趣(b:Infrastructure.CorspolicBuilder)->b.AllowAnyHeader()|>忽略;b.AllowAnyMethod()|>忽略))|>忽略
app.usegraffe webApp
let configureServices(服务:IServiceCollection)=
设sp=services.BuildServiceProvider()
设env=sp.GetService()
让viewsFolderPath=Path.Combine(env.ContentRootPath,“视图”)
您是否正在执行从不同域到localhost:5000的请求(例如,使用angularserve命令)?看起来您正在处理。我的客户端也是本地主机,但端口号不同。难道我不能在我的本地机器上测试web应用程序和单独的流程服务吗?如果您运行
dotnet new giraffe
,它还将在默认模板中为您自动配置Cors,因此您始终可以创建一个新的应用程序来快速查找它是如何完成的。
let private registrationHandler =
fun(context: HttpContext) ->
async {
let! data = context.BindJson<RegistrationRequest>()
match register data with
| Success profile -> return! json profile context
| Failure -> return! (setStatusCode 400 >=> json "registration failed") context
}
let private registrationHandler =
fun(context: HttpContext) ->
async {
return! text "hello world" context
}
POST >=>
choose [
route "/register" >=> registrationHandler
]
services
.AddCors()
.AddAuthentication(authScheme)
.AddCookie(cookieAuth)
|> ignore