views:

134

answers:

2

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?

+1  A: 

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.

Praveen Angyan
About question 1 - Isn't there a way for me not too need to repeat those lines again and again?
Samuel Carrijo
@samuelcarrijo check out my answer. might help.
Arnis L.
I'm just curious - what kind of expensive setup controller might have?
Arnis L.
+1  A: 
  1. 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.

  2. I would prefer writing actions in controllers they are supposed to be
    (i.e. GetProductByIdAsJson in Product controller) and if needed - check if it's partial request.

Just keep in mind that filters can't be generic. For me - it's a huge drawback.

Arnis L.