How to create ASP.Net Web API

How to create a ASP.Net Web API ?

In this blog I have created ASP.Net web api using Visual Studio 2015.
If you need to know what is web api then please read one of my previous blog.

Open Visual Studio 2015 and follow below steps:
1. File -> New Project and Select ASP.Net Web Application as shown in below screen.

create asp.net web api

2. Now select Web Forms and cheked Web API

create asp.net web api

3. Now your solutions explorere will look like this.

create asp.net web api

4. Now right click on "Controllers" folder and add new controller

create asp.net web api

5. Select Empty WEB API controller.

create asp.net web api

6. Now give a name to your controller please do not remove word "Controller". For eg as per below screen shot, you may only change "Default" to your desire name. In this example I have renamed controller name to TestController.

create asp.net web api

Now you have added a controller to your project, you may write your code in controller file.

public class TestController : ApiController

    {

public string InsertData(Entity ade)

        {

            var response = Call some Method

            return response;

        }


public HttpResponseMessage GetDataByID(int id)

        {


            DataSet ds = Call some Method

            if (ds != null)

                return Request.CreateResponse(HttpStatusCode.OK, ds);

            else

                return Request.CreateResponse(HttpStatusCode.NoContent, "null");

        }


public HttpResponseMessage GetAllData()

        {

            DataSet ds = Call some methods

           

            if (ds != null)

                return Request.CreateResponse(HttpStatusCode.OK, ds);

            else

                return Request.CreateResponse(HttpStatusCode.NoContent, "null");

        }



public string UpdateData(Entity ae)

        {

            string status = Call some method

            return status;

        }


public string DeleteData(int id)

        {

            string status = Call Some Method

            return status;

        }


}



Now you need to define the Route for your APIs else you will get error for "multiple actions found"

In WebApiConfig.cs  -- Comment existing/default route and write below code

config.Routes.MapHttpRoute(

                name: "GetDataByID",

                routeTemplate: "api/ad/GetDataByID/{id}",

                defaults: new { Controller = "Test", Action = " GetDataByID " }

            );


            config.Routes.MapHttpRoute(

               name: "GetAllData",

               routeTemplate: "api/ad/GetAllData",

               defaults: new { Controller = "Test", Action = " GetAllData " }

           );


            config.Routes.MapHttpRoute(

                name: "InsertData",

                routeTemplate: "api/{controller}/{id}",

                defaults: new { Controller = "Test", Action = " InsertData " }

            );


      config.Routes.MapHttpRoute(

                name: " UpdateData ",

                routeTemplate: "api/{controller}/{id}",

                defaults: new { Controller = "Test", Action = " UpdateData " }

            );



You may add more as per your requirement.
Please subscribe/comment if it is helpful to you.

Happy Coding :)

You may read some popular blogs on SharePointCafe.Net


Comments

Popular

SharePoint Interview Questions and Answers

Calling ASP.Net WebMethod using jQuery AJAX

What is Cloud Computing - A guide for beginners

How to Crop and Upload image using JQuery