Reputation: 11642
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?
Upvotes: 3
Views: 5575
Reputation: 18586
I believe you can do this using a HttpModule.
http://msdn.microsoft.com/en-us/library/aa719858%28VS.71%29.aspx
http://www.netfxharmonics.com/2007/08/Real-World-HttpModule-Examples.aspx
new link Address:
http://www.netfxharmonics.com/n/2007/08/real-world-httpmodule-examples
http://www.15seconds.com/Issue/020417.htm
Upvotes: 2
Reputation: 13083
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
Upvotes: 5