ASP.NET MVC Web API

Ranjit Singh January 7, 2019

ASP.NET MVC Web API

What is ASP.Net web API ?

ASP.NET Web API is an extensible framework for developing the HTTP based services, it can be accessed by diverse platforms like web, windows, mobile, etc.

Web API Version:

Version Framework Supported In
Web API 1.0 .Net 4.0 VS 2010
Web API 2- Current .Net 4.5 VS 2012,2013, 2015

In the ASP.NET MVC structure, there are various project types available and ASP.NET Web API is one of them. So, when we create a new application in ASP.NET, we need to select project type before we start working on it.

How to set up a new project:

  • Open the Visual Studio and click File → New → Project menu option. A new project dialog opens.
  • From the left pane, select Templates → Visual C# → Web.
  • In the middle pane, select ASP.NET Web Application
  • Select the “Empty” option and check the Web API checkbox in the ‘Add folders and core references for’ section and click OK.”

To create a web API controller class is an easy job. For adding a controller in MVC structure you need to choose API controller with an empty read/write action.

It will auto-create Get, Post, Put and Delete methods in API. Apart from this, it will also create a new route into Global.asax.cs file with name RouteTable.

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

The running project will respond in XML format.
For example:

<ArrayOfString>
<string>value1</string>
<string>value2</string>
</ArrayOfString>

If we change the ‘Accept’ header then API will return the response in JSON format:

["value1","value2"]

Share your views/queries in comments and do share it with your friends if you find the article helpful.

Lets’s Talk

About your ideas and concept