Getting Started With ASP.Net Web API 2: Day 2


Before reading this article, I highly recommend reading the previous part:

    As we saw in my previous article about the Web API 2, we have learned some advantages and features of the Web API.

    In this article we will learn the ASP.NET Web API to do a MVC application. In a general way the ASP.NET Web API is bundled with a MVC project template in Visual Studio 2012 or 2013. There are two ways to work with the Web API, either choose MVC template and install all the necessary packages from the NuGet Package Manager or choose the Web API template. In both approaches, including the Web API in an ASP.NET web application project is equivalent because the project wizard simply installs the Web API from NuGet.

    The main difference between MVC and the Web API is that since version 2 of the framework, the Web API handler, HttpControllerHandler, is a subclass of HttpTaskAsyncHandler, whereas the MVC version, MvcHandler, implements IHttpAsyncHandler directly. HttpTaskAsyncHandler is .NET 4.5 only, that is the only .NET version supported by Web API 2.

    When we run both, the MVC and the Web API in the same ASP.NET process, ASP.NET will use the HttpApplication. The MapRequestHandler event determines which of the HTTP handlers will be selected to handle the incoming request. At this scenario, the route matching happens and the request flows through the IRouteHandler relevant to the selected route. The sole purpose of IRouteHandler is to produce an IHttpHandler that can handle the request. If the IRouteHandler is HttpControllerRouteHandler (Web API route), then the Web API path will be chosen and the request will end up in the HttpControllerHandler. Conversely, if the route handler is MvcRouteHandler, then the MVC path takes over via MvcHandler.


    There are the following things we need to use when developing a Web API application:

    • Visual Studio 2013
    • ASP.NET Web API

    Getting Started

    So, let's proceed with the following sections:

    • Create ASP.NET Web Application
    • Adding Web API 2 Controller

    Create an ASP.NET Web API 2 Application

    Under this section, we will create an application based on the Web API 2 with MVC. There are some basic steps that we need to follow.

    Step 1

    Open Visual Studio 2013 and click New Project.

    Step 2

    Select the ASP.NET Web Application and provide a nice name for the application.


    Step 3

    In this step, we need to choose Web API template and hit the Enter key or Ok button.

    Step 4 

    Add a class file for student and define the properties of student.


    1. namespace Practice3.Models  
    2. {  
    3.     public class Student  
    4.     {  
    5.         public int Id { getset; }  
    6.         public string Name { getset; }  
    7.         public string Address { getset; }  
    8.     }  
    9. }  
    Step 5

    Add another class for adding some records using a List.
    1. namespace Practice3.Models  
    2. {  
    3.     public static class Students  
    4.     {  
    5.         public static List<Student> List = new List<Student>
    6.         {  
    7.             new Student{Id=1, Name="rajeev Ranjan", Address="Noida"},  
    8.             new Student{Id=2, Name="Nimit", Address="Noida"},  
    9.             new Student{Id=3, Name="Anil", Address="Noida"},  
    10.             new Student{Id=4, Name="Manish", Address="Noida"},  
    11.             new Student{Id=5, Name="Upankar", Address="Noida"},  
    12.             new Student{Id=6, Name="Papon", Address="Noida"},  
    13.             new Student{Id=7, Name="Anurag", Address="Noida"}  
    15.         };  
    16.     }  
    17. }  

    Step 6 

    Add two controllers, one for MVC and another for the Web API. The MVC controller has two Views.

    • Code for MVC controller
      1. namespace Practice3.Controllers.Mvc  
      2. {  
      3.     public class StudentsController : Controller  
      4.     {  
      5.         // GET: Students  
      6.         public ActionResult Index()  
      7.         {  
      8.             return View(Students.List);  
      9.         }  
      10.         public ActionResult Details(int id)  
      11.         {  
      12.             var stud = Students.List.FirstOrDefault(x => x.Id == id);  
      13.             if (stud == nullreturn new HttpNotFoundResult();  
      14.             return View(stud);  
      15.         }  
      16.     }  
      17. }  
    • Code for Web API Controller
      1. namespace Practice3.Controllers.WebApi  
      2. {  
      3.     public class StudentsController : ApiController  
      4.     {  
      5.         public Student GetById(int id)  
      6.         {  
      7.             var stud = Students.List.FirstOrDefault(x => x.Id == id);  
      8.             if (stud == nullthrow new HttpResponseException(HttpStatusCode.NotFound);  
      9.             return stud;  
      10.         }  
      11.     }  
      12. }  

    Step 7

    In this step we will write some codes for displaying  Web API data in the web page.

    Index Page

    1. @model System.Collections.Generic.List<Practice3.Models.Student>   
    2. @{   
    3. ViewBag.Title = "Title";   
    4. }   
    6. <dl>   
    7. @foreach (var stud in Model)   
    8. {   
    9. <dt>@stud.Id. @stud.Name . @stud.Address</dt>   
    10. <dd>@Html.ActionLink(stud.Name, "Details", "students", new {id=stud.Id},null)</dd>   
    11. }   
    13. </dl>  
    Details Page 
    1. @model Practice3.Models.Student  
    2. @{  
    3.     ViewBag.Title = "Details";  
    4. }  
    5. <div class="jumbotron">  
    6.     <h1> @Model.Name</h1>  
    7.     <p class="lead">@Model.Address</p>  
    8.     <p><a class="btn btn-default" href="/api/Students/@Model.Id"> Link »</a></p>  
    9. </div>  

    As we hit F5 to run the application, we can see the output, something like this: 


    After hitting the link button we can see the output: 


    If we wish to see the data in JSON format then we need to hit the link button, as shown in the output screen, then we can see the JSON format of a specific row.



    In this article we have learned the basic steps for creating a Web API in MVC. From future articles we will be able to understand more about the Web API.

    Further Readings

    << Getting Started With ASP.Net Web API 2 : Day 1                  >> Getting Started With ASP.NET Web API 2 :Day 3

    Up Next
      Ebook Download
      View all
      View all