Wednesday, October 16, 2013

Attributes in WebApi

In this post, we will see how we can create attributes and apply them in various api methods. Create a new WebApi application as we’d discussed in previous posts. Next create a class say, WriteLog which inherits the abstract class ActionFilterAttribute.

   1: using System;
   2: using System.Collections.Generic;
   3: using System.Linq;
   4: using System.Web;
   5: using System.Web.Http.Filters;
   6:  
   7: namespace TestApiServer.classes
   8: {
   9:     public class WriteLog : ActionFilterAttribute 
  10:     {
  11:  
  12:     }
  13: }

Please note, use the class ActionFilterAttribute from the System.Web.Http.Filter namespace and not from System.Web.Mvc, where former is used in WebApi and later is used in MVC.

Now if you look closely to the class ActionFilterAttribute, you will find that it has two methods:

  • OnActionExecuted
  • OnActionExecuting

While OnActionExecuted  method calls when the function over which we have tagged the attribute finishes processing its code, the OnActionExecuting method calls when the method is just beginning its code execution. So based on your requirement, you can override both the methods.

For our case however, we only need the “Executing" method, so that we can just log which method is being called. We therefore will just override the OnActionExecuting method.


   1: public class WriteLog : ActionFilterAttribute 
   2:     {
   3:         public override void OnActionExecuting(HttpActionContext actionContext)
   4:         {
   5:            //Do Something to write the log            
   6:         }
   7:     }
   8: }

And in order to apply this attribute to the method, we need to put [WriteLog] over the method, like:


   1: [WriteLog]
   2: public string Get()
   3: {
   4:     return LastValue;
   5: } 

Once you run the code and call the Get method, the OnActionExecuting method will be called before the method ‘Get’.

Further imagine if there is case when you are checking for some validation in ‘OnActionExecuting’ and if that fails, you need to stop the further processing and don’t want to allow the execution of the ‘Get’ method. In order to achieve that, you need to put something in ‘actionContext.Response’. If it is ‘null’ the method execution will continue, and if it is not null, the execution will stop. To visualize the scenario consider the following code.


   1: public override void OnActionExecuting(HttpActionContext actionContext)
   2: {
   3:   //Do Somthing to write the log    
   4:    actionContext.Response = new HttpResponseMessage() 
   5:                 { StatusCode = HttpStatusCode.BadRequest};
   6: }

In the above code block, I have modified the method to have a not null response message. Now since the Response is not null, the method ‘Get’ will not called, resulting in returning a BadRequest error to the user.

Happy Reading!!!

Share this post

0 comments

:) :-) :)) =)) :( :-( :(( :d :-d @-) :p :o :>) (o) [-( :-? (p) :-s (m) 8-) :-t :-b b-( :-# =p~ :-$ (b) (f) x-) (k) (h) (c) cheer

 
© 2013 Neelesh Vishwakarma
Posts RSS Comments RSS
Back to top