Capturing HTML output with a controller action filter

前端 未结 4 1983
温柔的废话
温柔的废话 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:42

    Try rewinding the stream to the beginning by setting Position = 0; before you read it.

    public override void OnActionExecuted(ActionExecutedContext filterContext)
    {
        stream.Position = 0;
        string response = new StreamReader(stream).ReadToEnd();
        ContentResult contres = new ContentResult();
        contres.Content = response;
        filterContext.Result = contres;
    }
    

提交回复
热议问题