之前,呈现给用户的MVC最后一次机会来改变响应 [英] MVC last chance to change response before it is rendered to user
本文介绍了之前,呈现给用户的MVC最后一次机会来改变响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要改变全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?
推荐答案
恕我直言,更好的方法来改变ASP.NET MVC环境HTML的反应是用行动过滤器。
这是COM pressing输出一个动作过滤器的例子:
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)
心连心
这篇关于之前,呈现给用户的MVC最后一次机会来改变响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文