Introduction to ASP. 2 (one)

Source: Internet
Author: User

Objective

HTTP is not just a Web page for service. It is also a powerful platform for building APIs that expose services and data. HTTP is simple, flexible, and ubiquitous. Almost any platform you can think of has an HTTP library, so the HTTP service can reach a wide range of customers, including browsers, mobile devices, and traditional desktop applications.

The ASP. NET Web API is a framework that is used to build Web APIs on the framework. In this tutorial, you will use the ASP. NET Web API to create a list of products returned by the Web API.

Create a Web API project

in this tutorial, you will use the ASP. NET Web API to create a list of products returned by the Web API. The front-end Web page uses JQuery to display the results.

When you start Visual Studio, select the new project from the start page. or, from the file menu, choose New and project .

in the Templates pane, select the installed template and expand nodes in Visual C # . in Visual C #, select Web. in the list of project templates, select the ASP. NET Web application . Name the item "Productsapp" and click OK .

in the new ASP . NET Project dialog box, select the empty template. Select the Web APIbased on Add folder and core reference files. Click OK . Such as

You can also create a Web API project that uses the Web API template. the Web API template uses ASP. NET MVC to provide a help page for the API. I used the empty template in this tutorial because I wanted to make it without the MVC Web API. in general, you do not need to know that the Web API is used in ASP.

Add a model

a model is an object that represents the data in your application. the ASP. NET Web API can automatically serialize your model to JSON, XML, or other formats, and then write the serialized data to the body of the HTTP response message. The object can be deserialized as long as the client can read the serialized format. Most clients can parse XML or JSON. In addition, the client can indicate which format it wants by setting the Accept header in the HTTP request message.

If the Solution Explorer is not visible, click the View menu, and then select Solution Explorer . In Solution Explorer, right-click the Model folder. from the context menu, select Add , and then select class .

name the Class "Product". Add the following properties to the  Product class.

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingsystem.web;namespacewebapidemo.models{ Public classProduct { Public intId {Get;Set; }  Public stringName {Get;Set; }  Public stringCategory {Get;Set; }  Public decimalPrice {Get;Set; } }}

Add Controller

In the Web API, the controller is an object that handles HTTP requests. We will add a controller that can return a list of products specified by the ID of a single product

Note If you already use ASP. NET MVC, you are already familiar with the controller. The Web API controller is similar to the MVC controller, but not the controller class Apicontroller class inheritance.

in Solution Explorer , right-click the Controller folder. Select Add , and then select Controller .

in the Add Scaffolding dialog box, select Web API controller-null . Click Add .

Scaffolding Create a ProductsController.cs file that is named in the Controller folder.

you do not need to put your controller into a folder named Controller. folder names are shortcuts to organizing your source files.

If the file is not open, double-click the file to open it. the code in this file is replaced by the following:

usingSystem.Linq;usingSystem.Net;usingSystem.Net.Http;usingSystem.Web.Http;usingWebapidemo.models;namespacewebapidemo.controllers{ Public classProductscontroller:apicontroller {product[] products=Newproduct[]{NewProduct{id=1, name="Toky1", category="USA 1", price=1},          NewProduct{id=2, name="Anglelaf", category="USA 2", price=3.75M} ,          NewProduct{id=3, name="sendnal", category="USA 3", price=16.99M}          };  PublicIenumerable<product>getallproducts () {returnProducts ; }         PublicIhttpactionresult GetProduct (intID) {varProduct = products. FirstOrDefault (P) =>p.id==ID); if(Product = =NULL) {                returnNotFound (); }            returnOk (product); }    }}
View Code

to keep the example simple, the product is stored in a fixed array, inside the controller class. of course, in an actual application, you would query the database, or use some other external data source.

The controller defines two methods for returning the product:

    • GetAllProductsmethod returns the product on the entire list as a IEnumerable < product > type.
    • GetProductmethod to find a single product through its ID.

That's it! You have a working network API. The methods on each controller correspond to one or more URIs:

Controller Method URI
Getallproducts /api/products
GetProduct /api/products/ID

Introduction to ASP. 2 (one)

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.