gpt4 book ai didi

nancy - 在 Nancy 2.x 中处理请求之前如何做某事?

转载 作者:行者123 更新时间:2023-12-05 06:21:15 25 4
gpt4 key购买 nike

我想在每个请求中做一些事情,无论是模块还是路由。我如何在 Nancy 2.x 中完成此操作?

如果找到How to Intercept all Nancy requestsHow do I capture all requests irrespective of verb or path , 但它们仅适用于 Nancy 1.x 和 Documentation已经过时了。

最佳答案

正如您所说,文档没有更新,您可以在网上找到的大部分资源都是针对 1.x 版的。

如何解决它有点取决于你想做什么。如果您没有弄乱响应,您可以像这样在 Bootstrap 中覆盖 ApplicationStartUp:

protected override void ApplicationStartup(TinyIoCContainer container, IPipelines pipelines)
{
pipelines.BeforeRequest.AddItemToEndOfPipeline((ctx) =>
{
Console.Out.WriteLine("Hit");
return null;
});
base.ApplicationStartup(container, pipelines);
}

另一方面,如果您需要干预响应和 header ,您可以在覆盖的 NancyModule 的构造函数中使用 Get 设置来完成,例如这个:

public InstrumentProgrammingNancyModule()
{
//// Enable CORS.
After.AddItemToEndOfPipeline((ctx) =>
{
ctx.Response.WithHeader("Access-Control-Allow-Origin", "*")
.WithHeader("Access-Control-Allow-Methods", "GET");
});

Get("/" , _ =>
{
return somethingOrOther;
});
....
}

这两种解决方案都适用于 Nancy 2.0。

关于nancy - 在 Nancy 2.x 中处理请求之前如何做某事?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59942732/

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