gpt4 book ai didi

c# - 实现选项 HttpVerb

转载 作者:行者123 更新时间:2023-11-30 22:08:46 35 4
gpt4 key购买 nike

我想接受来自角度网站的选项请求。每个端点(注册、登录等)都需要接受选项 http 动词。

然后我会将以下内容添加到响应 header 中。

   After += ctx =>
{
ctx.Response.WithHeader("Access-Control-Allow-Origin", "*");
ctx.Response.WithHeader("Access-Control-Allow-Headers", "accept, client-token, content-type");
ctx.Response.WithHeader("Access-Control-Allow-Methods", "POST, GET");
ctx.Response.WithHeader("Access-Control-Max-Age", "30758400");
};

我不想做的是为每个端点添加额外的路由

    Post[path + "Login"] = x => Login();
Options[path + "Login"] = x => Login();

这将是大量样板代码。

有没有一种方法可以使用通配符路由拦截任何选项请求,以便我的所有端点都可以接受选项请求?

最佳答案

Nancy 对 OPTIONS 请求有隐式路由,即用户定义的 OPTIONS 路由尚未定义。参见 OptionsRoute供引用。

如果你想为 OPTIONS 请求自定义行为,你可以选择添加一个 AfterRequest 钩子(Hook):

protected override void ApplicationStartup(TinyIoCContainer container, IPipelines pipelines)
{
pipelines.AfterRequest += ctx =>
{
// This will always be called at the end of the request
if (ctx.Request.Method.Equals("OPTIONS", StringComparison.Ordinal))
{
ctx.Response.WithHeader("Access-Control-Allow-Origin", "*");
ctx.Response.WithHeader("Access-Control-Allow-Headers", "accept, client-token, content-type");
ctx.Response.WithHeader("Access-Control-Allow-Methods", "POST, GET");
ctx.Response.WithHeader("Access-Control-Max-Age", "30758400");
}
}
}

但我不太确定为什么您只将 CORS header 添加到 OPTIONS 响应中?

关于c# - 实现选项 HttpVerb,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22068456/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com