According to MSDN, Routing is how Web API matches a URI to an action. Web API 2 supports a new type of routing, called attribute routing.
Introduction
In this article, we will use attribute routing to
create a REST API for a collection of products.For a general overview of attribute routing, see previous articles
Part 1 ,
Part 2Objective
The Objective of the article is to create a REST API using Attribute Routing in WEB API 2
Let start creating the project from scratch through step by step procedure
- Step 1 Create a New Project Open visual studio 2013 -> click on File -> New Project -> Create new ASP.NET Web Application.
In the Templates pane, select Installed Templates and expand the Visual C# node. Under Visual C#, select Web. In the list of project templates, select ASP.NET MVC Web Application. Name the project "ProductsAPI".
In the New ASP.NET Project dialog, select the Empty template. Under “Add folders and core references for”, select the Web API checkbox. Click Create Project.
This creates a outline project that is configured for Web API functionality.
- Step 2 Next, add classes for domain models. In Solution Explorer, right-click the Models folder. Select Add, then select Class. Name the class Manufacturer.
Add the following properties to the model class as shown
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
namespace ProductsAPI.Models
{
public class Manufacturer
{
public int ManufacturerId { get; set; }
[Required]
public string Name { get; set; }
}
}
Add another class named Product and add the following properties as shown below
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace ProductsAPI.Models
{
public class Product
{
public int ProductId { get; set; }
[Required]
public string Title { get; set; }
public decimal Price { get; set; }
public string Category { get; set; }
public DateTime ReleaseDate { get; set; }
public string Description { get; set; }
public int ManufacturerId { get; set; }
[ForeignKey("ManufacturerId")]
public Manufacturer Manufacturer { get; set; }
}
}
- Step 3 In this step, we'll add a Web API controller that uses Entity Framework as the data layer.
Press CTRL+SHIFT+B to build the project. Entity Framework uses reflection to discover the properties
of the models, so it requires a compiled assembly to create the database schema.
In Solution Explorer, right-click the Controllers folder. Select Add, then select Controller.
In the Add Scaffold dialog, select “Web API 2 Controller with read/write actions, using Entity Framework.”
In the Add Controller dialog, for Controller name, enter "ProductsController". Select the "Use async controller actions" checkbox. For Model class, select "Product". (If you don’t see the Product class listed in the dropdown, make sure that you built the project.) Then click the “<New data context...>” button.
Click Add in the New Data Context dialog.
Click Add in the Add Controller dialog. The scaffolding adds a class named ProductsController that defines the API controller. It also adds a class named ProductsAPIContext in the Models folder, which defines the data context for Entity Framework as shown below
Conclusion
In this article we have seen how to create the outline of the project to create a REST API,In coming articles we will see the remaining part
Reference
http://www.restapitutorial.com/