当前位置:  开发笔记 > 编程语言 > 正文

如何读取和更改Response的主体到覆盖OnActionExecuted方法的Controller

如何解决《如何读取和更改Response的主体到覆盖OnActionExecuted方法的Controller》经验,为你挑选了1个好方法。

我需要为控制器的每个动作的每个JSON答案附加一些信息.为此,我使基本控制器继承自标准MVC Controller类.我试着这样.但我抓住ArgumentException了这样的信息"Stream不可读".当ActionResult对象将数据写入正文时,可能会关闭流.我能做什么?

public override void OnActionExecuted(ActionExecutedContext context)
{
    var respApi = new ResponseApiDTO();
    respApi.Comment = "You could!!!";
    JObject jRespApi = JObject.FromObject(respApi);
    Stream bodyStream = context.HttpContext.Response.Body;
    JObject jbody;

    context.Result = Json(jbody);
    using( var rd = new StreamReader(bodyStream))
    {
        string bodyText = rd.ReadToEnd();
        jbody = (JObject)JsonConvert.DeserializeObject(bodyText);
        jbody.Add(jRespApi);
        context.Result = Json(jbody);
    }
}

trueboroda.. 6

我找到了解决方案.我们需要在管道中执行MVC步骤时将Body流替换为MemoryStream对象.然后我们必须将原始流对象对象返回到Response.Body.

// Extension method used to add the middleware to the HTTP request pipeline.
    public static class BufferedResponseBodyExtensions
    {

        public static IApplicationBuilder UseBufferedResponseBody(this IApplicationBuilder builder)
        {
            return builder.Use(async (context, next) =>
            {
                using (var bufferStream = new MemoryStream())
                {
                    var orgBodyStream = context.Response.Body;
                    context.Response.Body = bufferStream;

                    await next();//there is running MVC

                    bufferStream.Seek(0, SeekOrigin.Begin);
                    await bufferStream.CopyToAsync(orgBodyStream);    
                    context.Response.Body = orgBodyStream;
                }
            });
        }
    }

然后将其包含在pipline中.类启动,方法配置.

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
        {   
           ...
            app.UseBufferedResponseBody();
            app.UseMvc();           
           ...
        }

在替换响应正文流后,您可以通过结果过滤器读取和修改正文内容.

 public class ResultFilterAttribute : Attribute, IResultFilter
    {
        public async void OnResultExecuted(ResultExecutedContext context)
        {
            Stream bodyStream = context.HttpContext.Response.Body;
            bodyStream.Seek(0, SeekOrigin.Begin);
            string bodyText = rd.ReadToEnd();
        }
     }

必须将此属性应用于目标Controller类.



1> trueboroda..:

我找到了解决方案.我们需要在管道中执行MVC步骤时将Body流替换为MemoryStream对象.然后我们必须将原始流对象对象返回到Response.Body.

// Extension method used to add the middleware to the HTTP request pipeline.
    public static class BufferedResponseBodyExtensions
    {

        public static IApplicationBuilder UseBufferedResponseBody(this IApplicationBuilder builder)
        {
            return builder.Use(async (context, next) =>
            {
                using (var bufferStream = new MemoryStream())
                {
                    var orgBodyStream = context.Response.Body;
                    context.Response.Body = bufferStream;

                    await next();//there is running MVC

                    bufferStream.Seek(0, SeekOrigin.Begin);
                    await bufferStream.CopyToAsync(orgBodyStream);    
                    context.Response.Body = orgBodyStream;
                }
            });
        }
    }

然后将其包含在pipline中.类启动,方法配置.

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
        {   
           ...
            app.UseBufferedResponseBody();
            app.UseMvc();           
           ...
        }

在替换响应正文流后,您可以通过结果过滤器读取和修改正文内容.

 public class ResultFilterAttribute : Attribute, IResultFilter
    {
        public async void OnResultExecuted(ResultExecutedContext context)
        {
            Stream bodyStream = context.HttpContext.Response.Body;
            bodyStream.Seek(0, SeekOrigin.Begin);
            string bodyText = rd.ReadToEnd();
        }
     }

必须将此属性应用于目标Controller类.

推荐阅读
放ch养奶牛
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有