1. The.net Core first meeting

    .net Core is an open source generic development framework that supports cross-platform development and deployment on Windows, macOS, and Linux, and can be used in hardware devices, cloud services, and embedded/iot solutions.

    More information about.net Core is available hereMicrosoft Official DocumentationTo view, specific. Net Core introduction address:Docs.microsoft.com/zh-cn/dotne…

  2. .NET Core simply uses WebApi– create a project development tool: VS2019

    To create the WebApi project, enter the keyword Core in the template input box, then locate the ASP.NET Core Web application below and select it, then click next, as shown in the image below:

    Then enter the project name and solution name and click Create, as shown below:

    Then select the API option, uncheck HTTPS configuration, and click Create, as shown below:

    The created project file is shown below:

    At this point, the creation of the project is complete.

  3. .net Core simply uses WebApi- Create API In the second step above, we have created the WebApi project, the next step is to create a test API interface document.

    Before new Api documents, we can be in the project WeatherForecastController. Cs and WeatherForecast. Cs file deletion (when creating the project VS for our files to create an instance of the default). Next we create a new SysUserController that operates like ASP.NET and creates a SYS_User entity file inside the controller (formal environment should not put these two class files together) as follows:

    using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; namespace Quber.NetCore.WebApi.Controllers { [Route("api/[controller]")] [ApiController] public class SysUserController : ControllerBase {/// <summary> // GET request /// </summary> /// <returns></returns> [HttpGet] [Route(" getUsers ")] public List<SYS_User> GetUserList() { var ret = new List<SYS_User>(); for (int i = 0; i < new Random().Next(1, 20); i++) { var model = new SYS_User { UserName = "User-" + (i + 1), UserAge = new Random().Next(10, 100), CrtTime = DateTime.Now }; if (i % 2 ! = 0) { model.SortNo = i + 1; } ret.Add(model); } return ret; } / / / < summary > / / / POST request / / / < summary > / / / < param name = "data" > < param > / / / < returns > < / returns > [HttpPost] [Route("uploaduser")] public SYS_User GetUserList([FromBody] SYS_User data) { return data; }} /// <summary> /// entity file /// </summary> public class SYS_User {public string UserName {get; set; } public int UserAge { get; set; } public DateTime CrtTime { get; set; } public int? SortNo { get; set; } public int Count => 10 + UserAge; }}Copy the code

    In the code above, we can see that there is one on top of the controllerRouteRouting attributes ofAs the name impliesSet the route access address of the APIWhere [controller] stands for the name of the controller (of course, we can also add [action] to the routing address, which can be flexibly configured according to actual needs), that is, the address of the API is:http://ip/api/sysuser/…

    At the same time, in the above code, we define two interface methods, one is GET request, the other is POST request, and also set their routing address:

    The API request address for the GET method should be:http://ip/api/sysuser/getusers

    The API request address for the POST method should be:http://ip/api/sysuser/uploaduser

    At this point we can press Ctrl+F5 to view, and then change the browser address to:http://localhost:2101/api/sysuser/getusers, press Enter, the result is as follows:

    Don’t know if you have found that in the interface returned data, field properties of the first letters into lowercase, at the same time to return the value of time field, T actually contains a character that is not what we want to format, so if we want to keep the original entity case format output, and the time we want to format, it need to be configured as follows:

    We need to installMicrosoft.AspNetCore.Mvc.NewtonsoftJsonDependency packages, as shown below:

    Then add the following code Settings to the ConfigureServices method in startup. cs:

    / / use dependency package Microsoft. AspNetCore. Mvc. NewtonsoftJson provides methods to set the format of the returned data services. AddControllers () AddNewtonsoftJson (config = > { / / Ignore the circular reference config. SerializerSettings. ReferenceLoopHandling = ReferenceLoopHandling. Ignore; / / do not use the hump config. SerializerSettings. ContractResolver = new DefaultContractResolver (); / / set the time format config. SerializerSettings. DateFormatString = "MM - dd yyyy - HH: MM: ss". / / / / such as field to null values, this field will not return to the front / / config. The SerializerSettings. NullValueHandling = NullValueHandling. Ignore; });Copy the code

    At this point, we are regenerating the runtime API, and return the data format we want, as shown below:

  4. .NET Core simply uses WebApi– deploy and publish

    Right-click the selected project, select Publish, select folder in the pop-up interface, and click Next, as shown in the picture below:

    Select the folder address you want to publish and click Finish, as shown below:

    Then in the next interface, click the edit button behind the portable, and select the deployment mode as framework-dependent in the pop-up interface (of course, you can also select independent, after selecting independent, the published file will be large, and all the dependent DLL files to be used will be packaged and released). The target runtime is selected as portable (selected here to indicate the operating system platform to which the project is released)

    At this point, the preparatory work before the release is finished, then click the publish button to publish, as shown below:



    Deploy the release project WebApi project

    We’re only going to deploy against IIS, but to deploy against IIS, you need toInstall the ASP.NET Core Runtime module, go to the following address to download and install:

    Download address:Dotnet.microsoft.com/download/do…

    After the installation, you need to restart the IIS service, as shown in the following figure:

    To see which modules are installed on your computer, type dotnet –info in CMD:

    After you have installed the ASP.NET Core Runtime module, open the IIS manager and double-click the module. You will find one more moduleAspNetCoreModuleV2 moduleThis is IIS deployment. Net Core WebApi required modules, as shown below:

    Following the same steps as deploying the Web project to IIS, which is skipped here, we can access the interface using the IIS deployed domain name or IP, as shown in the following figure:

    At this point,.net Core WebApi is deployed on IIS. Of course, it can also be deployed on the console or independently. In practical applications, it is recommended to use IIS for deployment.

Finally the enclosed Demo source code download address: download.csdn.net/download/qu…