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


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

In this article we will learn the validation against your models in Web API 2 and share some validation logic with ASP.NET MVC. Web API 2 supports the same validation mechanism as ASP.NET MVC validation through attributes from System.ComponentModel.DataAnnotations. This is quite good to learn Web API 2 validation against the models with validation attributes and the framework will respect them.

The validation behavior of ASP.NET Web API 2 is the same across multiple hosts. We try to demonstrate the validation mechanism in two ways. The first way is using DataAnnotations and the second is using a third-party library called FluentValidation. This library can be downlaoded using the NuGet package Manager.


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

  • Visual Studio 2013
  • ASP.NET Web API 2

Getting Started

In this section we will follow some important procedures and these are:

  • Create ASP.NET Web API
  • Add Web API 2 Controller
  • Add a class inside the model and use Validation inside the Model class

We need to use a basic procedure to do it when we use the validation in the Model class. Let's learn how to use DataAnnotation in the model class.

Step 1

Open the Visual Studio 2013 and click New Project.

Step 2

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


Step 3

Select the Web API template and click the OK button, by default it will choose MVC along with the Web API.


Step 4

Right-click on the Model folder and add a Student class to the model where we define all the necessary fields. In the following image, I have defined some fields like Id, name, Address and Number.


  1. public class Student    
  2. {    
  3.     public int Id { getset; }    
  4.     [Required(ErrorMessage="Name is required")]    
  5.     [MaxLength(30)]    
  6.     public string Name { getset; }    
  7.     [Required(ErrorMessage="Address is required")]    
  8.     [MaxLength(120)]    
  9.     public string Address { getset; }    
  10.     [Required(ErrorMessage="Phone number is required")]    
  11.     [Range(0,12)]    
  12.     public string Number { getset; }   
  13. } 

As we can see, we are free to combine the data annotations with custom logic from within the Validate method. In this specific example Required and other annotation we took for the validation.

Note: Use the DataAnnotation namespace to import it before using the validation. For examle using System.ComponentModel.DataAnnotations;

Step 5

Right-click on the Controller folder and select the Web API 2 empty controller and provide a nice name.

  1. public Student Post( Student st)  
  2.         {  
  3.             if (ModelState.IsValid)  
  4.             {  
  5.                  throw new HttpResponseException(Request.CreateErrorResponse( HttpStatusCode.BadRequest,ModelState));  
  6.             }  
  7.             return st;  
  8.         }  
Another way to use validation is using a third-party library. The name of that Library is FluentValidation.

We can install this package using the NuGet command for downloading the package as:

Install-package FluentValidation


In this article we learned about the validation in the Web API 2 and the process of validation. We saw the validation using the ASP.NET MVC pattern as well as the third-party library.

Further Reading

Getting Started with ASP.NET Web API 2: Day 8 

Similar Articles