Creating weiapi with vs2019 and. Net core

Time:2020-10-31

Vs2019 creating webapi

1. Create a new project

 

 

2. Select ASP. Net core web application of. Net core

 

 

3. Define project name and storage location

 

 

4. Select API to create project

 

 

 

5. Delete the useless classes

 

 

 

 

6. Add a new method class

7. Set the route

using Microsoft.AspNetCore.Components;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace WEBAPI.Controllers
{

    /// 
    ///Set route
    /// 
    [Route("api/[controller]/[action]")]
    public class Calculation
    {


    }
}

 

 

 

8. Let’s start writing the interface methods you need

We create a post and two get methods respectively

The method is as follows

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;

namespace WEBAPI.Controllers
{

    /// 
    ///Set route
    /// 
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class Calculation
    {

        /// 
        ///Get text
        /// 
        /// 
        [HttpGet]
        public ActionResult Get()
        {
            return "Hello World!";
        }
        /// 
        ///Add two numbers together
        /// 
        ///First number
        ///The second number
        /// 
        [HttpGet]
        public ActionResult Sum(int num1, int num2)
        {
            return num1 + num2;
        }
        /// 
        ///Subtracting two numbers
        /// 
        ///Parameters
        /// 
        [HttpPost]
        public ActionResult Subtract(Param param)
        {
            int result = param.num1 - param.num2;
            return result;
        }


    }

    /// 
    ///Parameters
    /// 
    public class Param
    {
        /// 
        ///First number
        /// 
        public int num1 { get; set; }
        /// 
        ///The second number
        /// 
        public int num2 { get; set; }
    }
}

 

9. Start debugging

 

 

Modify the path to API / calculation / get in debugging

Start running with a result of

 

 

Other methods add parameter modification by themselves

 

 

To facilitate the management and debugging of the API interface, I use the swagger framework. To run my interface visually

1. Add nuget package for the projectSwashbuckle.AspNetCore

 

 

 

 

2. Add the following code to the configureservices method of startup, register swagger generator, define a document, and set the annotation path of XML document

 

 

 

services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
            //Configure swagger
            //Register the swagger generator and define a swagger document
            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new OpenApiInfo
                {
                    Version = "v1",
                    Title: "interface document",
                    Description = "RESTful API"
                });
                //Set XML document annotation path for swagger
                var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
                var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
                c.IncludeXmlComments(xmlPath);
            });
            services.AddMvc(x => x.EnableEndpointRouting = false);

 

 

3. Add the following code to the configure method to enable

Middleware services use to generate swagger and swaggerui

 

 

 

//Enable middleware service to generate swagger
            app.UseSwagger();
            //Enable middleware service to generate swagger,指定Swagger JSON终结点
            app.UseSwaggerUI(c =>
            {
                c.SwaggerEndpoint("/swagger/v1/swagger.json", "Web App V1");
                c.RoutePrefix =  string.Empty ; // set root node access
            });
            app.UseMvc();

4. Then right-click the project to continue to select Properties and select the output path in the build

And check the XML document

 

5. Clear the debugging path

 

6. Direct start