Capturing HTML output with a controller action filter

前端 未结 4 1977
温柔的废话
温柔的废话 2020-12-14 13:17

I\'ve got the following filter in place on an action to capture the HTML output, convert it to a string, do some operations to modify the string, and return a ContentResult

4条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-14 13:30

    Can you verify that stream is not NULL in the OnActionExectuted-method? I'm not sure the state of the stream-variable is being stored through the process..

    Why don't you try to get the stream out of the filterContext:

    public override void OnActionExecuted(ActionExecutedContext filterContext)
    {
        var stream = filterContext.HttpContext.Response.Filter;
        string response = new StreamReader(stream).ReadToEnd();
        ContentResult contres = new ContentResult();
        contres.Content = response;
        filterContext.Result = contres;
    }
    

提交回复
热议问题