Start ASP. NET Web API 2 Tour, asp. netapi

Source: Internet
Author: User

Start ASP. NET Web API 2 Tour, asp. netapi

HTTP is not just a Web page. It is also a powerful API platform for public services and data. HTTP is simple, flexible, and ubiquitous. Almost all platforms you can think of will have an HTTP library, so the HTTP service can reach a wide range of clients, including browsers, mobile devices, and traditional desktop applications.

ASP. NET Web API is a Framework used to build a. NET Framework-based Web API. In this tutorial, you will use ASP. NET Web APIs to create a web API that returns a product list.

The software used in the tutorial includes:

Visual Studio 2013

Web API 2

Create a Web API Project

In this tutorial, you will use ASP. NET Web APIs to create a web API that returns to the product list. The front-end page displays the result using jQuery.

Open Visual Studio, create a project, and select ASP. NET Web application,

Select the "Empty" template and select "Web API" in the "core reference" option ",

You can also use the "Web API" Project template to create a Web API project. Web API templates use ASP. net mvc to provide API help pages. This tutorial selects an empty template because I cannot figure out MVC to demonstrate Web APIs. Generally, you do not need to know ASP. net mvc when using Web APIs.

Add Model

Model is used to present data in your program. ASP. NET Web APIs can automatically serialize your model to json, xml, or other formats, and then serialize the data to the body of the HTTP Response Message. After the client reads the corresponding serialization format, it can deserialize the object. Most clients can parse XML or JSON. In addition, the client can also specify the required data format through the HTTP request header.

Let's start to create a simple model to present a product. In the solution directory Models, add a "Product" class:

    public class Product    {        public int Id { get; set; }        public string Name { get; set; }        public string Category { get; set; }        public decimal Price { get; set; }    }

Add a Controller

In Web APIs, controller is used to process http requests. We add a controller to return the product list and return a single product based on the product id.

Note: If you have used ASP. net mvc, you should be familiar with controllers. The controller of Web API is similar to that of MVC. However, the controller of Web API inherits from ApiController, while the controller of MVC inherits from controller.

Add a controller to the Controllers in the solution directory and select "Web API 2 Controller-null" in the base frame ":

The ProductsController code is as follows:

using System;using System.Collections.Generic;using System.Linq;using System.Net;using System.Net.Http;using System.Web.Http;using ProductsApp.Models;namespace ProductsApp.Controllers{    public class ProductsController : ApiController    {        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 }         };        public IEnumerable<Product> GetAllProducts()        {            return products;        }        public IHttpActionResult GetProduct(int id)        {            var product = products.FirstOrDefault((p) => p.Id == id);            if (product == null)            {                return NotFound();            }            return Ok(product);        }    }}

Two methods are defined in the controller:

GetAllProducts returns the complete product list. The URI returned by this method is/api/products;

The GetProduct method returns a single product using the product id. The URI returned by this method is/api/products/id.

Want to know

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.