Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/36.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
asp.net MVC 3/4相当于response.filter_Asp.net_Asp.net Mvc 3_Response.filter - Fatal编程技术网

asp.net MVC 3/4相当于response.filter

asp.net MVC 3/4相当于response.filter,asp.net,asp.net-mvc-3,response.filter,Asp.net,Asp.net Mvc 3,Response.filter,我需要截取所有发送到浏览器的html,并替换其中的一些标记。这将需要在全球范围内并针对每个视图进行。在ASP.NET MVC 3或4中使用C#实现这一点的最佳方法是什么?过去,我在ASP.net Webforms中使用Global.asax(vb)中的“response.filter”完成了此操作 这将调用我创建的一个类,该类继承自system.io.stream,并遍历html以替换所有标记。 我不知道如何在ASP.NET MVC 4中使用C#实现这一点。正如您可能已经注意到的,我在MVC世界

我需要截取所有发送到浏览器的html,并替换其中的一些标记。这将需要在全球范围内并针对每个视图进行。在ASP.NET MVC 3或4中使用C#实现这一点的最佳方法是什么?过去,我在ASP.net Webforms中使用Global.asax(vb)中的“response.filter”完成了此操作

这将调用我创建的一个类,该类继承自system.io.stream,并遍历html以替换所有标记。
我不知道如何在ASP.NET MVC 4中使用C#实现这一点。正如您可能已经注意到的,我在MVC世界中是一个完全的新手。

您仍然可以在ASP.NET MVC中使用响应过滤器:

public class ReplaceTagsFilter : MemoryStream
{
    private readonly Stream _response;
    public ReplaceTagsFilter(Stream response)
    {
        _response = response;
    }

    public override void Write(byte[] buffer, int offset, int count)
    {
        var html = Encoding.UTF8.GetString(buffer);
        html = ReplaceTags(html);
        buffer = Encoding.UTF8.GetBytes(html);
        _response.Write(buffer, offset, buffer.Length);
    }

    private string ReplaceTags(string html)
    {
        // TODO: go ahead and implement the filtering logic
        throw new NotImplementedException();
    }
}
然后编写一个自定义操作筛选器,该筛选器将注册响应筛选器:

public class ReplaceTagsAttribute : ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        var response = filterContext.HttpContext.Response;
        response.Filter = new ReplaceTagsFilter(response.Filter);
    }
}
现在剩下的就是装饰您希望应用于此筛选器的控制器/操作:

[ReplaceTags]
public ActionResult Index()
{
    return View();
}

如果要应用于所有操作,请在global.asax中将其注册为全局操作筛选器。

答案是正确的,但是。在使用它一段时间后,我遇到了一个例子,当响应被分成许多部分时,html是不正确的

Part 1: 
<html>.....<labe

Part 2: 
l/>...</html>
第1部分:
........
此外,部分渲染可能会产生意外情况。他们的html也脱离了主流。 因此,我的解决方案是在所有流式处理完成后,使用Flush方法进行处理

    /// <summary>
    /// Insert messages and script to display on client when a partial view is returned
    /// </summary>
    private class ResponseFilter : MemoryStream
    {
        private readonly Stream _response;
        private readonly IList<object> _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<object> 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<object> detachMessages)
        {

            if (detachMessages.Count == 0)
                return null;

            var javascript = CustomJavaScriptSerializer.Instance.Serialize(detachMessages);

            return "$(function(){var messages = " + javascript + @";
// display messages
base.ajaxHelper.displayMessages(messages);
})";
        }
    }
//
///插入返回部分视图时要在客户端上显示的消息和脚本
/// 
私有类响应筛选器:MemoryStream
{
私有只读流\u响应;
私有只读IList_消息;
公共覆盖无效刷新()
{
//添加消息并删除
//在一个页面上为多个方法调用筛选器(BeginForm、RenderPartial…)
//这样我们就不需要多次添加它了
var html=MessageAndScript(_detachMessages);
var buffer=Encoding.UTF8.GetBytes(html);
_detachMessages.Clear();
_写入(缓冲区,0,缓冲区长度);
base.Flush();
}
公共响应过滤器(流响应、IList消息)
{
_响应=响应;
_detachMessages=detachMessages;
}
公共重写无效写入(字节[]缓冲区、整数偏移量、整数计数)
{
_写入(缓冲区、偏移量、缓冲区长度);
}
私有静态字符串MessageAndScript(IList detachMessages)
{
if(detachMessages.Count==0)
返回null;
var javascript=CustomJavaScriptSerializer.Instance.Serialize(detachMessages);
返回“$(function(){var messages=“+javascript+@”;
//显示消息
base.ajaxHelper.displayMessages(消息);
})";
}
}

检查此链接:此答案显示如何使用响应过滤器重写生成的HTML。筛选器仅在代码退出OnActionExecuting函数时显示为执行。在响应被实际写入之前,我如何访问流缓冲区?响应过滤器的关键是修改响应流。但为了能够修改它,必须首先写入这个流。这似乎不适用于Razor。我在尝试向响应添加筛选器时遇到了“不允许筛选”HttpException。@MaximV.Pavlov,据ASP.NET团队的一位成员说:。我经历过,当流分裂时,源代码中会出现很多奇怪的“?”符号,从而使页面看起来很奇怪。你遇到过这个吗?
    /// <summary>
    /// Insert messages and script to display on client when a partial view is returned
    /// </summary>
    private class ResponseFilter : MemoryStream
    {
        private readonly Stream _response;
        private readonly IList<object> _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<object> 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<object> detachMessages)
        {

            if (detachMessages.Count == 0)
                return null;

            var javascript = CustomJavaScriptSerializer.Instance.Serialize(detachMessages);

            return "$(function(){var messages = " + javascript + @";
// display messages
base.ajaxHelper.displayMessages(messages);
})";
        }
    }