asp.net MVC 3/4 equivalent to a response.filter

后端 未结 2 989
耶瑟儿~
耶瑟儿~ 2020-12-06 08:10

I am in a need to intercept all of the html that will be sent to the browser and replace some tags that are there. this will need to be done globally and for every view. wha

2条回答
  •  我在风中等你
    2020-12-06 08:38

    The answer is correct but. After using it for a while I came across a case when the response is split in many parts so that html is incorrect

    Part 1: 
    ........
    

    Also partial renders may make unexpected cases. Their html is out of the main stream too. So my solution is to do it in the Flush method after all streaming is done.

        /// 
        /// Insert messages and script to display on client when a partial view is returned
        /// 
        private class ResponseFilter : MemoryStream
        {
            private readonly Stream _response;
            private readonly IList _detachMessages;
    
            public override void Flush()
            {
    
                // add messages and remove
                // filter is called for a number of methods on one page (BeginForm, RenderPartial...)
                // so that we don't need to add it more than once
    
                var html = MessageAndScript(_detachMessages);
                var buffer = Encoding.UTF8.GetBytes(html);
                _detachMessages.Clear();
                _response.Write(buffer, 0, buffer.Length);
    
                base.Flush();
            }
    
            public ResponseFilter(Stream response, IList detachMessages)
            {
                _response = response;
                _detachMessages = detachMessages;
            }
    
            public override void Write(byte[] buffer, int offset, int count)
            {
                _response.Write(buffer, offset, buffer.Length);    
            }
    
            private static string MessageAndScript(IList detachMessages)
            {
    
                if (detachMessages.Count == 0)
                    return null;
    
                var javascript = CustomJavaScriptSerializer.Instance.Serialize(detachMessages);
    
                return "$(function(){var messages = " + javascript + @";
    // display messages
    base.ajaxHelper.displayMessages(messages);
    })";
            }
        }
    
        

    提交回复
    热议问题