Learn how to create a REST API with Attribute Routing in WEB API 2 PArt 2

Rama Sagar
Posted by in ASP.NET Web API category on for Beginner level | Points: 250 | Views : 4356 red flag

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 2 , create REST API with Attribute routing



Objective

The Objective of the article is to create a REST API using Attribute Routing in WEB API 2


In previous article we have seen how to create the outline of the project  to create a REST API..Lets continue with the remaining part

  • Step 1 From the Tools menu, select Library Package Manager, and then select Package Manager Console.In the Package Manager Console window, enter the following command:

     
     enable-migrations



The above command creates a Migrations folder and adds a new code file named Configuration.cs. 





  •  Step 2 Open the Configuration.cs file and add the following code to the Configuration.Seed method.


namespace ProductsAPI.Migrations
{
    using ProductsAPI.Models;
    using System;
    using System.Data.Entity;
    using System.Data.Entity.Migrations;
    using System.Linq;

    internal sealed class Configuration : DbMigrationsConfiguration<ProductsAPI.Models.ProductsAPIContext>
    {
        public Configuration()
        {
            AutomaticMigrationsEnabled = false;
        }

        protected override void Seed(ProductsAPI.Models.ProductsAPIContext context)
        {
            context.Manufacturers.AddOrUpdate(new Manufacturer[] {
        new Manufacturer() { ManufacturerId = 1, Name = "Apple, Samsung" },
        new Manufacturer() { ManufacturerId = 2, Name = "Sony, Micromax" },
        new Manufacturer() { ManufacturerId = 3, Name = "Toshibha, Videocon" },
        new Manufacturer() { ManufacturerId = 4, Name = "Hero, Honda" }
        });

            context.Products.AddOrUpdate(new Product[] {
        new Product() { ProductId = 1,  Title= "Macbook", Category = "Electronics", 
        ReleaseDate = new DateTime(2000, 12, 16), ManufacturerId = 1, Description =
        "An Apple Laptop.", Price = 14.95M }, 

          new Product() { ProductId = 1,  Title= "Xperia", Category = "Mobiles", 
        ReleaseDate = new DateTime(2000, 12, 11), ManufacturerId = 2, Description =
        "A Sony Mobile.", Price = 14.95M }, 

          new Product() { ProductId = 1,  Title= "Grand Flip cover", Category = "Covers", 
        ReleaseDate = new DateTime(2000, 12, 10), ManufacturerId = 2, Description =
        "A Samsung Product.", Price = 14.95M }, 

          new Product() { ProductId = 1,  Title= "Canvas", Category = "BudgetMobiles", 
        ReleaseDate = new DateTime(2000, 10, 11), ManufacturerId = 3, Description =
        "A Micromax Mobile.", Price = 14.95M }, 

        new Product() { ProductId = 1,  Title= "Toshibha", Category = "Harddisk", 
        ReleaseDate = new DateTime(2000, 11, 11), ManufacturerId = 4, Description =
        "A Toshibha hardisk.", Price = 14.95M }, 
    });
        }
    }
}

  •  Step 3  Again in the Package Manager Console window, type the following commands.
add-migration Initial

update-database
  




Conclusion


In this article we have seen how to add packages and the context configuratio.n In coming articles we will see the remaining parts.

Reference

http://www.restapitutorial.com/

Page copy protected against web site content infringement by Copyscape

About the Author

Rama Sagar
Full Name: RamaSagar Pulidindi
Member Level: Silver
Member Status: Member,MVP
Member Since: 12/30/2012 1:51:40 AM
Country: India
ramasagar
http://www.ramasagar.com
A Software Profesional working in Microsoft .NET technologies since year 2008, and I work for Dake ACE. I am passionate about .NET technology and love to contribute to the .NET community at Dot Net Funda

Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)