storuwa.lk

How to Create Web API in ASP .NET MVC

Here we will discuss step by step how to create simple Web API using ASP .NET MVC and C#.

Web API stands for Application Programming Interface that is used to enable communication or interaction between software applications. ASP.NET Web API is a framework that makes it easy to build HTTP services that reach a broad range of clients, including browsers and mobile devices. By using ASP .NET, Web API can enable communicating by different devices from the same database.

Step 01

First, open your Visual Studio and on the start window click on “Create a new project”. Select “ASP .NET Web Application” template and give the project name, location, and the solution name for your ASP.NET Web Application.

Step 02

Then after, select Web API in the template window. Next, select create.

Step 03

This will create a new ASP.NET  web project in Visual Studio and you have to wait for some time till Visual Studio restores the packages in your project.

Step 04

Now, in Solution Explorer, go to the controller folder and expand it. You can see the “ValuesController.cs” file in it. It is the main class for Web API that is created by default. 

Right-click the Controllers folder. Select Add and then select Controller just like the below screen.

Select “Web API 2 Controller – Empty” and click the “Add” button from the “Add Scaffold” window.

In the Add Controller dialog box, name the controller as “DemoController“. Click Add.

The scaffolding creates a file named DemoController.cs in the Controllers folder.

Here you can see the Controller is inherited by the “ApiController” abstract class.

Step 05

Here, we will add a simple method in Controller to build and run your application ass shown below. In Web API, we use the method name as “Get ()”. You may give any appropriate method name.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace WebApi.Controllers
{
    public class DemoController : ApiController
    {
        public string Get()
        {
            return "Welcome To STORUWA.LK";
        }
        public List<string> Get(int Id)
        {
            return new List<string> {
                "Data1",
                "Data2"
            };
        }
    }
}
URL Format to run Web API

We should run the Web API “api/Controller_Name” URL format. For example, in MVC we define URL format using “RouteConfig” class and “RegisterRoutes” static methods. The same for Web API; we are using the “WebApiConfig” and “Register” static method. Go to the “Global.aspx” file we can see in detail.

We define the Web API URL format using “GlobalConfiguration” and “Configure” static method. We are passing the “Register” method from the “WebApiConfig” class as an argument into the “Configure” method. If we go to the “WebApiConfig” class, we can see Web API routes.

Step 06

Click on IIS Express to run your project. This will open the browser and run the above-mentioned URL format. Our Controller name is “Demo”, so we will run the project as shown below URL.

“https://localhost:44341/api/Demo”

It will display following result.

When we run https://localhost:44341/api/Demo/1, it will call “Get (int id)”. When there is a parameter in the method, the output looks like below.

So in this way you can create a simple Web API in ASP .NET MVC.

Exit mobile version