Introducing ASP.Net Web API 2: Day 1

Introduction

Today, I am creating an article series based on the ASP.NET Web API 2. Well, if we are talking about the Web API then HTTP is the most powerful platform for creating a Web API.

We can broadcast the service and data using the Web API. The use of HTTP is very easy and formative. As we know that every platform can understand the HTTP so that it can be accessible from everywhere like applications created on desktops, mobiles and as well as browsers.

Therefore, we can say that the ASP.NET Web API is a framework for building Web APIs on top of the .NET Framework. In this article, I'll describe the simple and easy use of the Web API with which we can fetch the data and show the data in the browser.

Features

The ASP.NET Web API has very good features that are defined below:

  • Attribute Routing
  • CORS
  • OWIN

Attribute Routing

Now the Web API 2 supports a new type of routing that is called the attribute routing. Now we can define the routes with the use of attributes. It provides more control over the URIs of the Web API.

CORS

Generally, browsers do not allow cross-domain capabiluty. CORS stands for Cross Origin Resource Sharing. Now ASP.NET Web API 2 supports the CORS with which we can make the AJAX call to two different domains.

OWIN

The acronym of the Open Web Interface is OWIN. The ASP.NET Web API 2 has the new functionality to self-host the application with the use of OWIN self host. The OWIN self host process is the better way for hosting the application rather then the IIS hosting.

Getting Started

So, let's proceed and create the application using Web API 2 with the following sections:

  • Creating Empty Project using Web API
  • Adding Model

Creating Empty Project using Web API

In this section we'll create the ASP.NET empty web application with the use of the Web API Project Template. So, use the following procedure:

Step 1: Open Visual Studio 2013 and click on New Project.

Step 2: Select the ASP.NET Web Application and enter the name as in the following:

Creating Web Application

Step 3: Select the Empty Project Template and select the Web API option.

Creating Empty Project using Web API Template

Note: You can select the Web API project template to create the application that gives you the MVC environment to work on it. I am selecting the Empty project here.

Step 4: The empty project solution now has the Models and Controllers folder and you can see it in the Solution Explorer.

Solution Explorer

Now we're done with the first section. Let's move to the next section to add a model.

Adding Model

In this section, we'll see how to add an ADO.NET Entity Model to the solution and start to work on it. We can create the simple class for the model but I am selecting an entity model that is from the local database. At first we select the database for the Local Database and select the table also. You can create the new database and table also. So, just use the following procedure.

Selecting Database

Step 1: Go to the Server Explorer, right-click on the Data Connection to select the Add Connection

Add Connection to Database in Server Explorer

Step 2: I am selecting the local database server and existing database, you can select another server and database also.

Defining Connection Properties

That's it. You can see that the connection is created. We can show the  database and table is also created.

Database on the Server Explorer

Adding ADO.NET Entity Data Model

Step 1: In the Solution Explorer, right-click on the Models folder and select the ADO.NET Entity Data Model to add it

Adding New ADO.NET Entity Data Model

Step 2: Specify the name for the entity model.

Specifying Model Name

Step 3: Now, select the option that is used for the model and click on Next.

Selecting Model Contents in Entity Model

Step 4: In the next wizard, the database connection string is selected already, you do not need to create a new connection. If it is not, then select the appropriate database.

Creating Data Connection in Entity Model

Step 5: Select the version of Entity Framework.

Selecting Entity Framework Version in Entity Model

Step 6: Select the database objects and specify the model namespace.

Selecting Database Objects in Entity Model

Step 7: When you click on the Finish, the Student entity model is created. Have a look:

Student Entity

In this entity model, there are four fields defined. A class with four properties are created automatically. Have a look:

  1. namespace StudentApiApp.Models  
  2. {  
  3.     using System;  
  4.     using System.Collections.Generic;  
  5.       
  6.     public partial class Student  
  7.     {  
  8.         public int ID { getset; }  
  9.         public string Name { getset; }  
  10.         public string Sex { getset; }  
  11.         public string City { getset; }  
  12.     }  
  13. }  

 

You can find this class in the Models folder. That's it for the model section.

Summary

This article provided a basic introduction to the ASP.NET Web API and the features of the Web API. We have also learned how to add an entity model to work with the database. In the next article we'll learn how to add an API Controller and call the controller using jQuery. Thanks for reading the article.

Up Next
    Ebook Download
    View all
    Learn
    View all