Monday, 11 December 2017

Web Api Basics and call from angularjs

1. First we have to create controller

[RoutePrefix("api/sample")]
 public class sampleSummaryController : ApiController
{
        [Route("sampleSummary/{id1}/{id2}/{id3}")]
         [HttpGet]
         public HttpResponseMessage GetVechileSummary(String id1, String id2,string id3)
         {
             if (string.IsNullOrEmpty(id1))
             {
                 return Request.CreateErrorResponse((HttpStatusCode)422, "Unprocessable Entity:  id1 ID is missing");
             }
             if (string.IsNullOrEmpty(id2))
             {
                 return Request.CreateErrorResponse((HttpStatusCode)422, "Unprocessable Entity:  id2 is  missing");
             }
             if (string.IsNullOrEmpty(id3))
             {
                 return Request.CreateErrorResponse((HttpStatusCode)422, "Unprocessable Entity:  id3 is missing");
             }

             var response = _sampleService.GetSampleSummary(webId, vin, locale);

             return Request.CreateResponse(HttpStatusCode.OK, response);
         }
}


2. call this from angularjs

return $http.get("http://localhost:53172/api/sample/sampleSummary/"+ 2+ "/" + 3+
"/"+3).then(function (result) {
return result.data;
});

No comments:

Post a Comment