Developing ASP. WEBAPI Learning notes with visual Studio Code (ii)--Web Api Demo

Source: Internet
Author: User
Tags response code


Add an ASP. NET Core MVC Package


1. Add the MICROSOFT.ASPNETCORE.MVC package to the Project.json file


 
 1 {
 2   "version": "1.0.0-*",
 3   "buildOptions": {
 4     "debugType": "portable",
 5     "emitEntryPoint": true
 6   },
 7   "dependencies": {
 8     "Microsoft.NETCore.App": {
 9       "type": "platform",
10       "version": "1.0.0"
11     },
12     "Microsoft.AspNetCore.Server.Kestrel": "1.0.0",
13     "Microsoft.AspNetCore.Mvc": "1.0.0"
14   },
15   "frameworks": {
16     "netcoreapp1.0": {
17       "imports": "dnxcore50"
18     }
19   }
20 }


2. Restore the newly added package to a local using dotnet restore in the cmd window





Ii. Modify the contents of the Startup.cs file and add ASP. NET MVC Capabilities
 
1 using Microsoft.AspNetCore.Builder;
  2 using Microsoft.Extensions.DependencyInjection;
  3
  4 namespace WebApiFrame
  5 {
  6 public class Startup
  7 {
  8 public void ConfigureServices(IServiceCollection services)
  9         {
10
11 // Inject MVC framework
12 services.AddMvc();
13 }
14
15 public void Configure(IApplicationBuilder app)
16 {
17 // Add MVC middleware
18 app.UseMvc();
19 }
20 }
twenty one }




III. Create a new controller and write a Web API method


1. Controller Userscontroller


 
1 using System;
 2 using Microsoft.AspNetCore.Mvc;
 3 using WebApiFrame.Models;
 4
 5 namespace WebApiFrame.Controller
 6 {
 7
 8 [Route("api/[controller]")]
 9 public class UsersController : Microsoft.AspNetCore.Mvc.Controller
10 {
11
12 [HttpGet("{id}")]
13 public IActionResult Get(int id)
14 {
15 var user = new User() { Id = id, Name = "Name:" + id, Sex = "Male" };
16 return new ObjectResult(user);
17 }
18
19 [HttpPost]
20 public IActionResult Post([FromBody] User user){
21 if(user == null){
22 return BadRequest();
twenty three             }
twenty four 
25 // TODO: Add operation
26 user.Id = new Random().Next(1, 10);
27 return CreatedAtAction("Get", new { id = user.Id }, user);
28 }
29
30 [HttpPut("{id}")]
31 public IActionResult Put(int id, [FromBody] User user){
32 if(user == null){
33 return BadRequest();
34 }
35
36 // TODO: Update operation
37 return new NoContentResult();
38 }
39
40 [HttpDelete("{id}")]
41 public void Delete(int id){
42 // TODO: delete operation
43
44 }
45 }
46 }


Unlike the previous ASP. NET. NET MVC version, the controller that implements the Web API in ASP. NET Core MVC inherits from the only one of the base class controllers.



2. Model User.cs


 
 
1 namespace WebApiFrame.Models
2 {
3     public class User
4     {
5         public int Id { get; set; }
6         public string Name { get; set; }
7         public string Sex { get; set; }
8     }
9 }


The final folder structure, such as








Iv. Start Debugging and debug the Web API interface using the Fiddler tool


1. GET Request






GET Response






2. POST Request






POST Response






A post response code of 201 indicates that the resource was created successfully.



In the response header there is a Location property, which is a navigation property, and the property value is a URL address that points directly to the resource address for which the post was just successful.



3. PUT Request






PUT Response






Put is an update operation. According to the specification, when the service update operation succeeds, it tells the client that the call is successful by the response code 204, and does not respond to the body by default.



4. DELETE Request






DELETE Response






Delete is the deletion operation. According to the specification, the response code needs to determine whether the success (200) or failure (500), the default does not respond to the body.



Developing ASP. WEBAPI Learning notes with visual Studio Code (ii)--Web Api Demo


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.