ASP. NET web API entry

Source: Internet
Author: User

Introduction

ASP. NET web API is a framework that simplifies the creation of HTTP Services.

It supports various clients, including browsers and mobile devices.

ASP. NET Web APIs are used to create restful applications on. NET Framework.ProgramIdeal Platform

For restful Web Services, see here: http://zh.wikipedia.org/wiki/REST

Preparation

ASP. net mvc 4 includes ASP. NET web API, please install here: http://www.asp.net/web-api

Visual Studio 2010 or Visual Studio 2012 can be used for development.

For the characteristics of ASP. NET web API, see here: http://www.asp.net/whitepapers/mvc4-release-notes#_Toc317096197

Introduction

HTTP is not just a web pages Service

It is also a powerful platform for creating network APIs.

These APIs provide network services and can interact with data.

HTTP protocol is simple and flexible. The most important thing is that it is everywhere.

Almost all of the platforms you can think of support the HTTP protocol

Therefore, the HTTP protocol can be compatible with most clients.

Including browsers, mobile clients, and desktop applications

ASP. NET web API is a class library for creating Web APIs on. NET Framework.

In this articleArticleMedium,

You will see how to create a web API using ASP. NET web API,

And let this API return the data of a product list

Create a project

As shown in:

Create model

A model is an object used to display data.

ASP. NET web API can automatically serialize Model Objects

JSON, XML, or other data formats

Then write the serialized data into the HTTP Response Message.

The client can read the serialized data.

And deserialize the data into an object.

Most clients can parse XML and JSON data.

In addition, you can determine the data formatting Method Based on the message header.

Create a model class named Product

CodeAs follows:

Using system; using system. collections. generic; using system. LINQ; using system. web; namespace hellowebapi. models {public class product {public int ID {Get; set;} public string name {Get; set;} Public String category {Get; set;} public decimal price {Get; set ;}}}

Create a controller

If you have used ASP. NET MVC

You will find the controller of ASP. NET web API

The Controller is basically the same as that of ASP. net mvc.

The biggest difference is that

The Controller of ASP. NET web API inherits from apicontroller

The Controller of ASP. net mvc inherits from the Controller.

The web API controller does not return view, but directly returns data.

It is not necessary to put the created controller class file in the Controller folder.

After the file is modified, the 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 hellowebapi. models; namespace hellowebapi. controllers {public class productscontroller: apicontroller {product [] products = new product [] {New Product {id = 1, name = "tomato soup", Category = "groceries ", price = 1.39 m}, new product {id = 2, name = "yo-yo", Category = "Toys", price = 3.75 m}, new product {id = 3, name = "Hammer", Category = "hardware", price = 16.99 M }}; public ienumerable <product> getallproducts () {return products;} public product getproductbyid (int id) {var Product = products. firstordefault (p) => P. id = ID); If (Product = NULL) {var resp = new httpresponsemessage (httpstatuscode. notfound); throw new httpresponseexception (RESP);} return product;} public ienumerable <product> getproductsbycategory (string category) {return products. where (p) => string. equals (P. category, category, stringcomparison. ordinalignorecase ));}}}

The getallproducts method returns an array of the product type.

GetproductbyidSearch for product by ID

Getproductsbycategory

So far

You can use the following URI to access the corresponding method:

Getallproducts:/API/products

Getproductbyid:/API/products/ID

Getproductsbycategory:/API/products /? Category =Category

Access Web APIs

Run this project,

Access the following URL to obtain the result.

Http: // localhost: 5380/API/products

Here we see the XML content

However, Web APIs are very intelligent.

Client requests can be used to determine the types of data to be transmitted.

We can use the following code to obtain JSON-type data:

$ (Document ). ready (function () {// send an Ajax request $. getjson ("API/products/", function (data) {// on success, 'data' contains a list of products. $. each (data, function (Key, Val) {// format the text to display. vaR STR = Val. name + ': $' + val. price; // Add a list item for the product. $ ('<li/>', {HTML: Str }). appendto ($ ('body '));});});});

Shows the obtained data.

Project source code:

Http://files.cnblogs.com/liulun/HelloWebAPI.zip

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.