In the last article [.net Core project practical development environment construction] mainly introduces the construction of the project development environment, this article mainly introduces the WebApi construction and integration Swagger under.net Core, and also introduces some of their own programming habits.

The project structure

The structure of the project is essentially the same.

  • Controller: Exposed contract

  • Business/IBussiness: Business logic layer implementation and interface

  • DataAccess: DataAccess layer

  • Model: Entity class library

  • Common: Public method class library

Swagger integration

The dependency package Swashbuckle.AspNetCore is first loaded via NuGet

Then right click on your project, under properties, tick Generate XML document file, Swagger will automatically parse the corresponding XML for matching.

Then start coding by adding the following code to startup. cs under ConfigureServices f:

services.AddSwaggerGen(c => { c.SwaggerDoc("v1", new Info { Title = "My API", Version = "v1" }); var basePath = PlatformServices.Default.Application.ApplicationBasePath; var xmlPath = Path.Combine(basePath, "MyDemo.xml"); var xmlPath1 = Path.Combine(basePath, "MyDemo.Model.xml"); c.IncludeXmlComments(xmlPath); c.IncludeXmlComments(xmlPath1); });

Copy the code

Here I also load the Xml for the Model layer so that the descriptions of the corresponding fields can also be displayed on the API document.

Add the following code to the Configure method:

app.UseSwagger(); App.useswaggerui (c=>{c. waggerEndpoint("/swagger/v1/swagger.json", "My API v1 "); });

Copy the code

Configuration here, you can directly run to see the effect, the result after running found error as follows:

That is because the main project did not reference the Model class library, so the corresponding XML was not produced in the debug of the main project. Under the reference, the production is run again, and the corresponding effect can be seen by entering /swagger.

Remember that the project was only 1.0.0 when it was introduced, but the style of the 2.3.0 version has changed a lot.

So I can start the default page with /swagger, right click docker-compose and change the url:

The code

Net Core supports most of the common attributes and methods, so feel free to write them.

We recommend to use “await” and “async”, but note that async must be done to avoid blocking.

Here’s a quick example:

Public static async Task<ResponseResult> Delete(long id){var result = new ResponseResult(); try { using (var conn = DatabaseManager.GetConnection(DatabaseManager.DBName)) { await conn.OpenAsync(); string sql = $@"DELETE FROM USER WHERE Id=@Id"; await conn.ExecuteAsync(sql, new { Id = id, UpdateTime = DateTime.Now }); }} the catch (Exception ex) {/ / log return new ResponseResult {Result = false, Code = ResponseCode UnknownException, ErrorMessage = ex.Message }; } return result; }

Copy the code

Public static async Task<ResponseResult> Delete(long id){if (id <= 0) return ResponseResult {Result = false, ErrorMessage = "illegal" did not pass into the primary key or primary key, Code = ResponseCode. NeedsKeyParameter}; return await UserDataAccess.Delete(id); }

Copy the code

//Controller[HttpDelete("{id}")]public async Task<IActionResult> Delete(long id){ return AssertNotFound(await UserService.Delete(id)); }

Copy the code

Because it is a demonstration, static classes and static methods are used here, and interfaces are also omitted, so you can choose the most appropriate one according to your needs in the actual project.

conclusion

Net Core is a convenient way to develop Web APIS.

Long click the QR code to follow us