1.Web Project
1.1 Overview
The previous example, we "toss" the operation is the string, obviously it is not commonly used. This time we play with the entity class, after all, object-oriented development requires entity classes to store and transfer data.
1.2 Creating a Project
As in the previous example, create a project:
1.3 Adding entity classes
Under the Models folder, add the Product.cs class, whose code:
namespace webapi04.models{public class Product {public int ID {get; set;} public string Name {get; set;} public string Genre {get; set;}} }
1.4 Adding a Controller
Under the Controllers folder, (delete ValuesController.cs) add ProductsController.cs, whose code:
Using system.collections.generic;using system.web.http;using webapi04.models;using system.linq;namespace webapi04.controllers{public class Productscontroller:apicontroller {//Analog data static list<product& Gt Products = Initlist (); private static list<product> initlist () {var List = new List<product> { New Product{id=1,name= "Lenovo", genre= "Computer"}, new Product{id=2,name= "Apple 5s", genre= "Phone"}, New Product{id=3,name= "HP500", genre= "Computer"}, new Product{id=4,name= "Samsung Note4", genre= " Mobile "}, New Product{id=5,name=" IBM X86 ", genre=" PC "}, new product{id=6,name=" Cock Wire ", genr E= "forcing"}; return list; }//Get:api/products//<summary>///</summary>//<return S> Product Collection </returns> public ienumerable<product> GETPROducts () {return products; }///<summary>////</summary>//<param name= "id" > Product Id</para m>//<returns> single product </returns> public product Get (int id) {var product = ( From P in the products where p.id = = ID Select p). FirstOrDefault (); return product; } }}
1.5 Query All
After you run the Web site, open the Fiddler tool:
View results:
1.6 Querying a single
1.7 Query single non-existent
Query id=100, which is not in the analog data.
It returns the result is NULL or hint illegal, can its status code result, or 200 (if the previous operation character Set example, the program will prompt the index range error, the status code is not 200), obviously unreasonable.
Preferably not present, the status code is 404.
Modify the get (int id) in ProductsController.cs, whose code:
If the query individual entity does not exist, return a 404 Status Code///<summary>////</summary>/// <param name= "id" > Product id</param> ///<returns> Response message </returns> public httpresponsemessage Get (int id) { var product = (from P in products where p.id = = ID Select p). FirstOrDefault (); if (product = null) { return request.createresponse<product> (Httpstatuscode.ok, product); } else { return request.createerrorresponse (Httpstatuscode.notfound, "does not Exist");} }
you need to add the following namespaces: using system.net.http;using system.net;
Once again, test the query:
Modify the get (int id) in ProductsController.cs, and you can also use the following methods (the result is not):
Returns 404 status Code [Responsetype (typeof (Product))] public ihttpactionresult Get (int id) { var if query single entity does not exist) Product = (from P in the products where p.id = = ID Select p). FirstOrDefault (); if (product = null) { return NotFound (); } return Ok (product); }
Need to introduce using System.Web.Http.Description;
1.8 Add a
In ProductsController.cs, add the following code:
POST api/values///<summary>//Add products///</summary>// <param name= "Product" > Product entities </param> ////<returns> response Messages </returns> public httpresponsemessage Post ([Frombody] Product product) {products . ADD (product); var msg = Request.createresponse (httpstatuscode.created); Msg. Headers.location = new System.Uri (Request.requesturi + (products). Count). ToString ()); return msg; }
To run the test:
The result:
1.9 Modifying views
We used the Fiddler tool to see how the data is displayed on the Web page.
Home\index.cshtml Code Modification:
<ul id= "prod" ></ul> @section scripts{ <script> $.ajax ({ URL: '/api/products ', Success:function (data) { var produclist = $ (' #prod '); for (var i = 0; i < data.length; i++) { var product = Data[i]; Produclist.append (' <li> ' + product. Name + ' </li> ');}} ) </script>}
Browse Home:
1.10 Adding a default page
The above uses Razorle, we can also directly use static HTML page, in the root directory to add defaut.html, its code:
<! DOCTYPE html>Browse this page:
(Note: The code volume is higher with Jquey request and bind data.) Later tutorials will use Knockout.js for example)
2. Summary
What can you learn in this case? I hope you will be interested to continue watching. Of course, there is no way to delete and modify the method, you can experience the example of the previous writing.
Webapi Learning Note 04: Using the Webapi template--entity class--Status code--view