ASP.NET-MVC – MVC lasts the opportunity to change the response to the user

I need to change the complete html response stream (using html parsing) before it is presented to the user.
Where/when is the last chance?
IMHO, a better way to change the HTML response in the ASP.NET MVC environment is to use action filters.
This is an example of an action filter used to compress output:

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

string acceptEncoding = request.Headers["Accept-Encoding"];
< br /> 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 on your action method:

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

HTH

I need Change the complete html response stream (using html parsing) before it is presented to the user.
Where/when is the last chance?

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

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("Conte nt-encoding", "deflate");
response.Filter = new DeflateStream(response.Filter, CompressionMode.Compress);
}
}
}

< p>You can use it on your action method:

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

HTH

Leave a Comment

Your email address will not be published.