MVC 在呈现给用户之前更改响应的最后机会

发布于 2024-08-19 11:48:25 字数 61 浏览 5 评论 0原文

我需要在呈现给用户之前更改完整的 html 响应流(使用 html 解析)。 最后一次机会在哪里/什么时候?

I need to change full html response stream (with html parsing) before it is rendered to user.
Where/When is the last chance to do that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

时光瘦了 2024-08-26 11:48:25

恕我直言,在 ASP.NET MVC 环境中更改 HTML 响应的更好方法是使用操作过滤器。
这是用于压缩输出的操作过滤器的示例:

public class CompressFilter : ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        HttpRequestBase request = filterContext.HttpContext.Request;

        string acceptEncoding = request.Headers["Accept-Encoding"];

        if (string.IsNullOrEmpty(acceptEncoding)) return;

        acceptEncoding = acceptEncoding.ToUpperInvariant();

        HttpResponseBase response = filterContext.HttpContext.Response;

        if (acceptEncoding.Contains("GZIP"))
        {
            response.AppendHeader("Content-encoding", "gzip");
            response.Filter = new GZipStream(response.Filter, CompressionMode.Compress);
        }
        else if (acceptEncoding.Contains("DEFLATE"))
        {
            response.AppendHeader("Content-encoding", "deflate");
            response.Filter = new DeflateStream(response.Filter, CompressionMode.Compress);
        }
    }
}

您可以在操作方法中像这样使用它:

    [CompressFilter]
    // Minifies, compresses JavaScript files and stores the response in client (browser) cache for a day
    public JavaScriptResult GetJavaScript(string jsPath)

HTH

IMHO, a better way to alter HTML response in ASP.NET MVC environment is to use action filters.
This is an example of an action filter for compressing the output:

public class CompressFilter : ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        HttpRequestBase request = filterContext.HttpContext.Request;

        string acceptEncoding = request.Headers["Accept-Encoding"];

        if (string.IsNullOrEmpty(acceptEncoding)) return;

        acceptEncoding = acceptEncoding.ToUpperInvariant();

        HttpResponseBase response = filterContext.HttpContext.Response;

        if (acceptEncoding.Contains("GZIP"))
        {
            response.AppendHeader("Content-encoding", "gzip");
            response.Filter = new GZipStream(response.Filter, CompressionMode.Compress);
        }
        else if (acceptEncoding.Contains("DEFLATE"))
        {
            response.AppendHeader("Content-encoding", "deflate");
            response.Filter = new DeflateStream(response.Filter, CompressionMode.Compress);
        }
    }
}

You can use it like this on your action methods:

    [CompressFilter]
    // Minifies, compresses JavaScript files and stores the response in client (browser) cache for a day
    public JavaScriptResult GetJavaScript(string jsPath)

HTH

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文