Friday, July 19, 2019

how to create a new ASP.NET Web API

This blog will demonstrates steps by steps to how to create a new ASP.NET Web API and explain its characteristics


ASP.NET Web API is a very light weight .net framework to create HTTP service and it is very easy to build restful service, which covers boarder client like mobile device, desktop and other platform.

ASP.NET Web API


Characteristic of Web API:
  • ASP.NET Web API creates Web API on top of ASP.NET and so it can use ASP.NET request/response pipeline.
  • ASP.NET Web API maps with HTTP verbs (GET, POST, PUT etc.) to Method Name.
  • It supports many response Data formatter (like JSON, XML, BSON).
  • It supports only HTTP protocol.
Here is 4 simple steps to create ASP.NET WEB API by using visual studio.

 Step 1: Open visual studio and click New Project:


 Select Visual C# a Web Asp.NET Web Application (.Net Framework) Template 


    ASP.NET Web Application

    Select Empty project template - it’s an empty project template for creating ASP.NET Web API project

    Web API Empty Project


    Here is Web API Empty Project - SimpleWebAPI


    ASP.NET Web API Project




    Step 2: Add Web API Controller 


    Select Web API 2 Controller – Empty - to create an empty Web API controller

      Add Web API Controller


      Give API Controller Name – TaskController.cs

        Give Web API Controller Name

        Here is TaskController Controller Generated Class 

        C# Code: TaskController.cs

        public class TaskController : ApiController
              {
                public TaskController()
                {

                }  
              }



        Step 3: Add Model Class –Task.cs

        Add Model Class


        Here is Model Class - Task.cs

        C# Code: Task.cs

        public class Task
               {
                public int TaskID { getset; }
                public string Description { getset; }

               }
            


           Step 4: Add API Controller Method – GetTaskByID

           C# Code: TaskController.cs

            public class TaskController : ApiController
            {
                public TaskController()
                {

                } 
              
                [HttpGet]
                public Task GetTaskByID(int taskID)
                {
                    //@Todo Logic to get task by ID
                    return new Task() {  TaskID = 1001, Description = "Todo Task"};
                }
            } 


            By default WebApiConfig.cs class contain web api routing settings
            C# Code: WebApiConfig.cs

              public static class WebApiConfig
            {
                public static void Register(HttpConfiguration config)
                {
                    // Web API configuration and services

                    // Web API routes
                    config.MapHttpAttributeRoutes();

                    config.Routes.MapHttpRoute(
                        name: "DefaultApi",
                        routeTemplate: "api/{controller}/{id}",
                        defaults: new { id = RouteParameter.Optional }
                    );
                }
            }


           It’s done J !! Your web API is ready for use.



          You can use google chrome postman tool to call web api 


        Postman

             Response Body:

        Postman Web API Response




             For API more interactive documentation you can use the Swagger UI and easily setup swagger for ASP.NET Web API

        Swagger UI

        No comments:

        Post a Comment

        Setup Swagger for ASP.Net Web API

        this blog demonstrates step by step to add swagger in web api project and will submit  http request GET/POST/PUT via swagger UI. Swagger ...