作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想在每个请求中做一些事情,无论是模块还是路由。我如何在 Nancy 2.x 中完成此操作?
如果找到How to Intercept all Nancy requests和 How 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/
我是一名优秀的程序员,十分优秀!