asp.net.core study notes 1: use of swagger and webapi receive Jobject object

  Environment: asp.net.core 3.1 (when awakening, the official does not recommend 3.0, so without any core experience, you can only start the development and learning of 3.1)

  Due to the separation of the front-end and back-end of the existing projects and the increasing popularity of microservices, instead of using web applications (razor pages, mvc), the webapi is used directly. Perhaps subsequent projects will use SignalR for real-time data. To return to the topic, the swagger commonly used in webapi must have a name, and the benefits will not be said, who knows who uses it.

  1. Use of swagger. There are many related tutorials on the Internet (including localization, etc.), which only briefly describe the basic steps and provide some tips for application.

       1. Add Swashbuckle.AspNetCore package reference in nuget manager;

  2. Add service configuration. Add the service configuration code of swagger in the ConfigureServices method in startup.cs. Pay attention to the path and name of the configuration xml. You need to configure the name and address in the project property "Generate".

1  services.AddSwaggerGen(c=> {
2                 c.SwaggerDoc("v1", new Microsoft.OpenApi.Models.OpenApiInfo { Title = "My API", Version = "v1" });
3                 c.IncludeXmlComments(System.IO.Path.Combine(System.AppContext.BaseDirectory, "API.xml"));
4             });

 

   3. Add swagger configuration. Add code in Configure in startup.cs to configure the application. The regular configuration of swagger endpoint is sufficient.

Tip : Set the swagger routing prefix to empty, and set the debugging startup path to index.html. Since the front-end and back-end are separated, the interface is not provided in the back-end service. Set index.html as the startup path. You can open the swagger interface at the first time of debugging.

1         app.UseSwagger();
2             app.UseSwaggerUI(c =>
3             {
4                 c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");
5                 c.RoutePrefix = "";
6             });

 

  Second, Webapi receives jobject objects. The use of json in api is very common, but core does not support weakly typed objects in api requests. What can be determined is that it supports custom types and basic data types. Usually use post to send a json. If json is a strong type that has been defined by api, then core can directly deserialize json into a custom type. But if the content of json is not fixed, or it is not easy to define a strong type, we usually choose the weak type of jobject, but unfortunately json deserialization of jobject is not supported before 3.0. (Core before 3.0 usually customizes a model binding builder and model binding method, reference 3)

       After 3.0, optimization support for jobject was added, the reference package Microsoft.AspNetCore.Mvc.Newtonsoft, and json support for the controller when configuring the service can be as follows:

  services.AddControllers().AddNewtonsoftJson();

  Then you can add post API in the controll.

 

 

 

 

 

 Interested comrades can study Reference 3 to see how core 1.1 implements model binding (there are flaws in the practice process, this ancient method is not recommended).

 

Reference materials:

1.https://lbadri.wordpress.com/2014/11/23/web-api-model-binding-in-asp-net-mvc-6-asp-net-5/

2.https://docs.microsoft.com/zh-cn/aspnet/core/tutorials/getting-started-with-swashbuckle?view=aspnetcore-3.1&tabs=visual-studio

3. https://www.cnblogs.com/showmu/p/6264950.html

Guess you like

Origin www.cnblogs.com/cnDqf/p/12700039.html