Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/307.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在返回视图之前获取NancyResponse内容正文_C#_Nancy - Fatal编程技术网

C# 在返回视图之前获取NancyResponse内容正文

C# 在返回视图之前获取NancyResponse内容正文,c#,nancy,C#,Nancy,是否有可能在返回视图之前获取NancyResponse主体 我的意思是: Get["/"] = x => { var x = _repo.X(); var view = View["my_view", x]; **//here I want to send the response body by mail** return view; }

是否有可能在返回视图之前获取NancyResponse主体

我的意思是:

Get["/"] = x => {
                var x = _repo.X();
                var view = View["my_view", x];
                **//here I want to send the response body by mail**
                return view;
            };
小心此答案基于nancy版本0.11,自那以后发生了很大变化。路由中的版本应该仍然有效。如果使用内容协商,则after管道中的一个将不可用

您可以将内容写入路由中的memorystream,也可以将委托添加到路由中 后管道:

public class MyModule: NancyModule
{
    public MyModule()
    {
        Get["/"] = x => {
            var x = _repo.X();
            var response = View["my_view", x];
            using (var ms = new MemoryStream())
            {
                response.Contents(ms);
                ms.Flush();
                ms.Position = 0;
                //now ms is a stream with the contents of the response.
            }
            return view;
        };

        After += ctx => {
           if (ctx.Request.Path == "/"){
               using (var ms = new MemoryStream())
               {
                   ctx.Response.Contents(ms);
                   ms.Flush();
                   ms.Position = 0;
                   //now ms is a stream with the contents of the response.
               }
           }
        };
    }
}

View[]
返回一个
响应
对象,该对象具有
内容
属性,类型为
操作
,因此您可以将
内存流
传递给代理,它将呈现该流中的视图

我使用Nancy版本0.17,@albertjan解决方案基于0.11。 多亏了@TheCodeJunkie,他把我介绍给了
Iviewrender

public class TheModule : NancyModule
{
    private readonly IViewRenderer _renderer;

    public TheModule(IViewRenderer renderer)
    {
           _renderer = renderer;

           Post["/sendmail"] = _ => {

                string emailBody;
                var model = this.Bind<MyEmailModel>();
                var res = _renderer.RenderView(this.Context, "email-template-view", model);

                using ( var ms = new MemoryStream() ) {
                  res.Contents(ms);
                  ms.Flush();
                  ms.Position = 0;
                  emailBody = Encoding.UTF8.GetString( ms.ToArray() );
                }

                //send the email...

           };

    }
}
public类模块:NancyModule
{
私有只读IViewrender\u渲染器;
公共TheModule(IViewrender渲染器)
{
_渲染器=渲染器;
Post[“/sendmail”]=\u=>{
字符串正文;
var model=this.Bind();
var res=_renderer.RenderView(this.Context,“电子邮件模板视图”,model);
使用(var ms=new MemoryStream()){
物质含量(ms);
弗拉什女士();
ms.Position=0;
emailBody=Encoding.UTF8.GetString(ms.ToArray());
}
//发送电子邮件。。。
};
}
}

我看到is有一个“AsAttachment”扩展,可以让浏览器下载响应。是的,我看到,它只覆盖了contet类型mmm我只是注意到没有ctx.response.Body流这里的响应似乎与handlergood中的响应相同,再次感谢,您认为如何将它直接放在Get[]处理程序中,而不是放在后面?放在那里的目的是什么?@JackNova更新了问题,将获取路线内的响应包括在内。Microsoft.CSharp.RuntimeBinder.RuntimeBinderException:“Nancy.Responses.Negotiation.Negotilator”不包含“内容”的定义