Reputation: 2302
1) Isn't it a bad practice to use these attributes on all of my Controllers?
[HandleError] [CompressFilter(Order = 1)]
Compress filter is defined as:
public class CompressFilter : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
if ( !AppConfig.IsResponseCompressionEnabled ) {
base.OnActionExecuting(filterContext);
return;
}
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);
}
}
}
2) What if i need to call some server functions from client, using ajax. Is it good idea, to create one special controller(named ie. "WebServiceController") for such a tasks?
Upvotes: 0
Views: 314
Reputation: 47587
I would apply filters on controller instead of applying them for every action.
And if they are truly needed for ALL actions (that might involve creation of another filter for exceptions) - there are techniques to apply them application wide (it might be a good idea to apply specified filters for controller through inheritance too).
I guess there are plenty of ways how to fix this problem - it's harder to find the right one which fits your needs exactly.
Just keep in mind that filters can't be generic. For me - it's a huge drawback.
Upvotes: 1
Reputation: 7265
Question 1
I don't see anything wrong with using the [HandleError] attribute on all your controllers. You want to be able to catch unhandled exceptions thrown from any action or result being processed.
The [CompressFilter] attribute is a bit trickier, but you should be able to get a great amount of compression for most HTML or text based responses. If you are responding with a binary file like a file attachment, you might not want to use that attribute. Finally, wait until you are getting enough traffic to see if the benefits of compression outweigh the cost.
Question 2
In general you don't want to have a controller just to serve AJAX requests. Use the controller that's most suited to the resource that is being requested by the AJAX call.
If your controller has any sort of expensive setup in it's constructor, then you can definitely look into setting up a separate controller to handle AJAX calls.
Upvotes: 1