实行期权HttpVerb [英] Implementing Options HttpVerb
问题描述
我想接受一个角度的网站选择请求。每一个端点(注册,登录等)需要接受的选项HTTP动词。
I want to accept options requests from an angular website. Every endpoint (signup, login etc) needs to accept the options http verb.
我会再添加以下的响应头。
I will then add the following to the response 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");
};
什么我不想做的就是添加一个额外的路由每个端点像
What i dont want to do is add an extra route for every endpoint like
Post[path + "Login"] = x => Login();
Options[path + "Login"] = x => Login();
这是群众的样板code。
This would be masses of boilerplate code.
有没有一种方法,我可以拦截任何选项要求使用通配符路径,使我所有的终端都能接受的选择要求?
Is there a way i can intercept any options request using a wildcard route so that all my endpoints can accept options requests?
推荐答案
南希具有选项
的要求,即一个用户定义的选项隐含路线
路线尚未确定。请参见 OptionsRoute
,以供参考。
Nancy has implicit routes for OPTIONS
requests, i.e. a user defined OPTIONS
route has not been defined. See OptionsRoute
for reference.
如果你想为选项自定义行为
的要求,你可以交替添加 AfterRequest
挂钩:
If you want custom behavior for OPTIONS
requests, you could alternatively add a 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头文件添加到选项
的反应?
这篇关于实行期权HttpVerb的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!