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

C# 南希:在AfterRequest事件中修改模型?,c#,nancy,C#,Nancy,我想在我的Bootstrapper.cs中添加一个AfterRequest事件处理程序,它能够在调用每个路由后修改响应上的模型。这可能吗?我在响应中没有看到任何可以访问模型的属性(如果有) 下面是我的示例用法(来自Bootstrapper.cs): 我认为没有那么简单,您应该检查ctx.Response.Content以了解使用了哪个反序列化程序以及返回的对象,我制作了一个简单的示例,返回一个序列化为Json的Foo对象 public class MyBootstrapper : Nan

我想在我的
Bootstrapper.cs
中添加一个
AfterRequest
事件处理程序,它能够在调用每个路由后修改
响应上的模型。这可能吗?我在响应中没有看到任何可以访问模型的属性(如果有)

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


我认为没有那么简单,您应该检查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; }
    }
公共类MyBootstrapper:Nancy.DefaultNancyBootstrapper
{
受保护的覆盖无效应用程序启动(TinyIoC.TinyIoCContainer容器、Nancy.Bootstrapper.IPipelines管道)
{
基础应用程序启动(容器、管道);
pipelines.AfterRequest+=修改模型;
}
私有void修改模型(NancyContext ctx)
{
富富,;
使用(var memory=new MemoryStream())
{
ctx.Response.Contents.Invoke(内存);
var str=Encoding.UTF8.GetString(memory.ToArray());
foo=JsonConvert.DeserializeObject(str);
}
ctx.Response.Contents=流=>
{
使用(var writer=新的StreamWriter(流))
{
foo.Code=999;
Write(JsonConvert.SerializeObject(foo));
}
};
}
}
公共类家庭模块:Nancy.NancyModule
{
公共家庭模块()
{
获取[“/”]=参数=>{
返回Response.AsJson(newfoo{Bar=“Bar”});
};
}
}
公开课Foo
{
公共字符串条{get;set;}
公共整数代码{get;set;}
}

在进一步研究之后,这对于Nancy框架来说是不可能的(至少在合理范围内是如此)。

如果您仍然需要此功能,您可能会对我刚刚在Nuget上发布的扩展感兴趣:。我们的项目中需要类似的功能

这将允许您在执行路由后修改模型。一定要看一下上面的描述


请告诉我这是否适合您的需要。

谢谢。我希望有更直截了当的东西。我有预感这是个白日梦。
    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; }
    }