Wednesday, February 21, 2018

Few things to remember on WebApi


1) Use the Attribute routing which is the best and easy way I guess you can use for routing and the best part is it avoids the confusion.
2) Use the [RoutePrefix("api/v1/new")] attribute on the controller so that you don't need to declare "api/v1/new"  inside each and every method you write.

    public class NewController : ApiController
    {
        [Route("api/v1/new/UserDetails")]
        [HttpGet]
        public HttpResponseMessage UserDetails()
        {
            return Request.CreateResponse(HttpStatusCode.OK, "Hello People.");
        }
    }
AFTER
    [RoutePrefix("api/v1/new")]
    public class NewController : ApiController
    {
        [Route("UserDetails")]
        [HttpGet]
        public HttpResponseMessage UserDetails()
        {
            return Request.CreateResponse(HttpStatusCode.OK, "Hello People.");
        }
    }
3) Never use the Http verbs (GET,POST,PUT,DELETE) in your method names like GetUserDetails instead use UserDetails().
4) Make use of the http tags like so it makes difficult for others to identify which verb you are using for your WebApi

        [Route("UserDetails")]
        [HttpGet] //like this one
        public HttpResponseMessage UserDetails()
        {
            return Request.CreateResponse(HttpStatusCode.OK, "Hello People.");
        }
5) Try to use the HttpResponseMessage as the return type.
6) WebApi by default returns the JSON format.
7) We can also override the RoutePrefix by using (~) symbol for ex-
    [RoutePrefix("api/v1/new")]
    public class NewController : ApiController
    {
        [Route("~/api/v1/old/Data")]
        [HttpGet]
        public HttpResponseMessage Data()
        {
            return Request.CreateResponse(HttpStatusCode.OK, "Hello People.");
        }   
   
     }

Now instead of using the http://localhost:1234/api/v1/new/Data use this http://localhost:1234/api/v1/old/Data because the root has been overridden
8) We can also add the parameters to the route and also give the default values
        [Route("DataByID /{ID}")] //Passing parameter
        [HttpGet]
        public HttpResponseMessage DataByID(int ID = 2) //Default value 2 will be passed if not specified any value.
        {
            if (ID == 1)
            {
                return Request.CreateResponse(HttpStatusCode.OK, "Hello People.");
            }
            else
            {
                return Request.CreateResponse(HttpStatusCode.BadRequest, "No Result Found");
            }

        }

9) We can also add the constraint on the WebApi methods
        [Route("DataByID /{ID:int}")] //The ID must be integer or else the method will not execute
        [HttpGet]
        public HttpResponseMessage DataByID(int ID)
        {
            if (ID == 1)
            {
                return Request.CreateResponse(HttpStatusCode.OK, "Hello People.");
            }
            else
            {
                return Request.CreateResponse(HttpStatusCode.BadRequest, "No Result Found");
            }

        }

No comments:

Post a Comment

Git Commands

Git Version   To check the git version Git -v       Git Clone To clone the repository, use the following command: Git clone [u...