南希:在AfterRequest事件中修改模型吗?

Nancy: Modify model in AfterRequest event?
2021-06-10
  •  译文(汉语)
  •  原文(英语)

我想向我添加一个AfterRequest事件处理程序,该事件处理程序Bootstrapper.cs能够在Response调用每条路由之后在模型上进行修改.这可能吗?我没有在Response上看到任何可以访问Model的属性(如果有).

这是我的示例用法(来自Bootstrapper.cs):

 protected override void ApplicationStartup(..., IPipelines pipelines)
 {
    ...
    pipelines.AfterRequest += ModifyModel;
 }

 private void ModifyModel(NancyContext ctx)
 {
    // do things to the response model here
 }
解决过程1

如果仍然需要此功能,您可能会对我刚刚在Nuget上发布的扩展感兴趣:https ://www.nuget.org/packages/Nancy.ModelPostprocess.Fody .我们的项目中需要类似的功能

这将允许您在执行路线之后修改模型.看一下Bitbucket页面上的描述

请告诉我这是否适合您的需求.

解决过程2

我认为不是那么简单,您应该检查ctx.Response.Content才能知道使用了哪个反序列化器以及要返回的对象,我做了一个简单的示例,返回了一个序列化为Json的Foo对象.

    public class MyBootstrapper : Nancy.DefaultNancyBootstrapper
    {
        protected override void ApplicationStartup(TinyIoC.TinyIoCContainer container, Nancy.Bootstrapper.IPipelines pipelines)
        {
            base.ApplicationStartup(container, pipelines);

            pipelines.AfterRequest += ModifyModel;
        }

        private void ModifyModel(NancyContext ctx)
        {
            Foo foo;
            using(var memory = new MemoryStream())
            {
                ctx.Response.Contents.Invoke(memory);

                var str = Encoding.UTF8.GetString(memory.ToArray());
                foo = JsonConvert.DeserializeObject<Foo>(str);
            }

            ctx.Response.Contents = stream =>
            {
                using (var writer = new StreamWriter(stream))
                {
                    foo.Code = 999;
                    writer.Write(JsonConvert.SerializeObject(foo));
                }
            };
        }
    }

    public class HomeModule : Nancy.NancyModule
    {
        public HomeModule()
        {

            Get["/"] = parameters => {
                return Response.AsJson<Foo>(new Foo { Bar = "Bar" });
            };
        }
    }

    public class Foo
    {
        public string Bar { get; set; }
        public int Code { get; set; }
    }
速聊1:
谢谢.我希望有一些更直接的方法.我预感这是白日梦.
解决过程3

在进行了更多研究之后,就不可能(至少在合理范围内)使用现有的Nancy框架来做到这一点.

I want to add an AfterRequest event handler to my Bootstrapper.cs that is able to modify the model on the Response after every route is invoked. Is this possible? I don't see any properties on the Response where I could gain access to the Model (if there is one).

Here is my example usage (from Bootstrapper.cs):

 protected override void ApplicationStartup(..., IPipelines pipelines)
 {
    ...
    pipelines.AfterRequest += ModifyModel;
 }

 private void ModifyModel(NancyContext ctx)
 {
    // do things to the response model here
 }
Solutions1

If you're still need this functionality you may be interested in an extension I just published on Nuget: https://www.nuget.org/packages/Nancy.ModelPostprocess.Fody. We needed a similar functionality in our project

This will allow you to modify your models after the route has already executed. Do have a look at the description on the Bitbucket page

Please tell me if this suits your needs.

Solutions2

I think is not that simple, you should inspect the ctx.Response.Content in order to know which deserializer is used and what object are you returning, I made a simple example returning a Foo Object Serialized as Json.....

    public class MyBootstrapper : Nancy.DefaultNancyBootstrapper
    {
        protected override void ApplicationStartup(TinyIoC.TinyIoCContainer container, Nancy.Bootstrapper.IPipelines pipelines)
        {
            base.ApplicationStartup(container, pipelines);

            pipelines.AfterRequest += ModifyModel;
        }

        private void ModifyModel(NancyContext ctx)
        {
            Foo foo;
            using(var memory = new MemoryStream())
            {
                ctx.Response.Contents.Invoke(memory);

                var str = Encoding.UTF8.GetString(memory.ToArray());
                foo = JsonConvert.DeserializeObject<Foo>(str);
            }

            ctx.Response.Contents = stream =>
            {
                using (var writer = new StreamWriter(stream))
                {
                    foo.Code = 999;
                    writer.Write(JsonConvert.SerializeObject(foo));
                }
            };
        }
    }

    public class HomeModule : Nancy.NancyModule
    {
        public HomeModule()
        {

            Get["/"] = parameters => {
                return Response.AsJson<Foo>(new Foo { Bar = "Bar" });
            };
        }
    }

    public class Foo
    {
        public string Bar { get; set; }
        public int Code { get; set; }
    }
Talk1:
Thanks. I was hoping for something more straight-forward. I had a hunch that this was a pipe dream.
Solutions3

After researching this more, this is simply not possible (at least within reason) to do with the Nancy framework as it exists today.

转载于:https://stackoverflow.com/questions/19095350/nancy-modify-model-in-afterrequest-event

本人是.net程序员,因为英语不行,使用工具翻译,希望对有需要的人有所帮助
如果本文质量不好,还请谅解,毕竟这些操作还是比较费时的,英语较好的可以看原文

留言回复
我们只提供高质量资源,素材,源码,坚持 下了就能用 原则,让客户花了钱觉得值
上班时间 : 周一至周五9:00-17:30 期待您的加入