Table of contents
1. Create aps.net core Web Aip
1. Create a new Asp.Net Core WebApi project
2. Try to start the Asp.Net Core WebApi project
2. Write a login interface: Login
3. Modify the port number in the appsettings.json configuration file
4. Run the program and test the custom API interface
3. Publish WebAip to the server
1. Publish to the server through the file system
2. Release via Docker (recommended)
The vs2022 introduced below is a simple Asp.Net Core WebAip based on .NET 6
1. Create aps.net core Web Aip
1. Create a new Asp.Net Core WebApi project
Start vs2022 and select "Create New Project (N)" on the start page.
Choose asp.net core WbeApi from C# as an initial WebApi template.
Click Next, give the project a project name, and choose the directory where we want to store the project.
Click Next, select the target framework of the project, vs2022 has long-term support for .NET 6.0, if you are vs2019 or other versions, you can also choose .NET 5.0 or .NET Core, and finally click Create.
After the created project, we can see that there are mainly program dependency package files (third-party file packages that need to be used later), controller code folder (code is placed here), configuration files (basic configuration information of the project), and program entry The file (also known as the Main function) consists of
2. Try to start the Asp.Net Core WebApi project
Click the start button to start the project in Debug mode
There will be an ssl certificate prompt at the first startup, just click yes
After startup, the browser opens the interface document generation tool page Swagger and a console page.
swagger shows our current interface: WeatherForecast
What the console runs is actually the MyWebApi.exe program generated after the project is compiled
2. Customize an API interface
1. Add a controller
Right click under the Controllers folder > Add > Controller
Add an empty controller:
Give the controller a name: AdminController.cs
2. Write a login interface: Login
The code of the AdminController.cs controller is as follows:
using Microsoft.AspNetCore.Mvc;
namespace MyWebApi.Controllers
{
[Route("api/[controller]/[action]")]
public class AdminController : Controller
{
[HttpGet]
public string Login(string pwd)
{
if(pwd == "123456")
{
return "登录成功!";
}
return "登录失败";
}
}
}
[Route("api/[controller]/[action]")] indicates the address of the custom api: /api/controller name/j interface name, taking the current controller as an example: api/Admin/Login?pwd=111
[HttpGet] indicates that the request is made in Get mode
3. Modify the port number in the appsettings.json configuration file
The appsettings.json code is as follows:
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*",
//配置端口号
"Kestrel": {
"Endpoints": {
"Https": {
"Url": "https://*:5000"
},
"Http": {
"Url": "http://*:5050"
}
}
}
}
The above configuration file modified the default port numbers to 5050 and 5000
4. Run the program and test the custom API interface
Start the program in debug mode:
Then enter the interface address in the browser: https://localhost:5000/api/admin/login
It can be seen that the interface has been connected. When the pwd parameter is not passed, the interface returns: login failed
Let's pass a correct pwd parameter to try: https://localhost:5000/api/admin/login?PWD=123456
The interface returns: Login succeeded! So far, a simple Asp.Net Core WebAip has been built
3. Publish WebAip to the server
1. Publish to the server through the file system
Right click on the project > Build
Generate successful output here will be prompted
Then right click on the project > open the folder in the file explorer and find the generated file system
Copy all the files in the folder to the server, and then run MyWebApi.exe through cmd in the current folder
If the above error occurs during operation, it prompts that the system must install the operating framework of .net 6
Open the download link prompted with a browser: Download ASP.NET Core 6.0 Runtime (v6.0.11) - Windows x64 Installerz
After downloading and installing, continue to run MyWebApi.exe. Under normal circumstances, the webapi will run normally.
2. Release via Docker (recommended)
Although .NET core implements cross-platform development and deployment, if it is published to a new server through the file system, it is still necessary to download and configure the .net-related operating framework. It is still difficult for novices, so it is recommended to use the docker method to generate directly The docker image can be used to directly run the webapi program by pulling the image from the server and running the image container.
Publishing via docker requires both the server and the local docker program to be installed. For the download and installation of docker, please refer to the official website: Docker: Accelerated, Containerized Application Development
For details on how to publish WebApi programs through docker, please refer to: https://blog.csdn.net/lwpoor123/article/details/127900973