Getting started with ASP.NET Web Api

This is Vijay Vardhan. I'm new to blogging. So please just allow me if i make mistakes in this post. Today i'm going to tell about how to start with ASP.NET Web Api.

Below are the steps to follow

1. Open Vistu Studio =>  File=>New => Project as shown in below image
Getting started with ASP.NET Web Api

2.  Then the Window will open as shown in below. Give the project name as "Getting Started with ASP.NET Web Api". Click on "ok".

Getting started with ASP.NET Web Api

3. Then the New ASP.NET web application window open as shown below. Choose "Empty" from ASP.NET Templates and click on "Web Api" below. Then click on "ok".

Getting started with ASP.NET Web Api

4. Now you have created a ASP.NET Web Api project. So now add Web Api controller in project. To add, right click on controller => Add => Controller as shown below.

Getting started with ASP.NET Web Api

5. Then "Add Scaffold" window open as shown below. Choose "Controller" installed, choose "Web Api controller with read/write actions" and click on "ok".


Getting started with ASP.NET Web Api
6. Now give name to Web Api controller as "WebApiController".

Getting started with ASP.NET Web Api




7. Now your Web Api controller is ready with read/write actions.

Getting started with ASP.NET Web Api
8. For understanding purpose create a model class and name it as "Products" and include the following code.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Gettting_Started_with_ASP.NET.Models
{
    public class Products
    {
        public class Product
        {
            public int Id { get; set; }
            public string Name { get; set; }
            public string Category { get; set; }
            public decimal Price { get; set; }
        }
    }
}

9. Include the model name space in your Web Api controller as "using static Gettting_Started_with_ASP.NET.Models.Products;". Now add the Products in your controller with this code.

Product[] products = new Product[]
       {
            new Product { Id = 1, Name = "Tomato Soup", Category = "Groceries", Price = 1 },
            new Product { Id = 2, Name = "Yo-yo", Category = "Toys", Price = 3.75M },
            new Product { Id = 3, Name = "Hammer", Category = "Hardware", Price = 16.99M }
       };

Now replace

  public List<Product> Get()
        {
            return products.ToList();
        }

        public List<Product> Get(int id)
        {
            return products.Where(x => x.Id == id).ToList();
        }

Finally your controller will look like this.




Getting started with ASP.NET Web Api

5. This is the final step and important step to get result from Web Api controller. Now place the debuggers in both GET methods and run the project. In the url, write "/api/webapi" after port number to hit first GET method (ex: "http://localhost:51222/api/webapi"). Now you will see all the products as shown below.

Getting started with ASP.NET Web Api

This result is related to GET method. Suppose if u want to get all the results related to id=1. We have to call GET method with id. In the url, write "/api/ webapi/1" after port number to get the products with id=1. It will show the results as below.

Getting started with ASP.NET Web Api

So now its done you have created ASP.NET Web Api controller and got the results from GET methods through browser. Some may get doubt like what about inserting, updating and deleting (post, put and delete). These can't be done through browser. That we will see in the next section. 

I hope this article may help you.All feed backs and suggestions are welcome.

Getting started with ASP.NET Web Api Getting started with ASP.NET Web Api Reviewed by Vijay Vardhan on 21:52:00 Rating: 5

No comments: