Core Web Api stepped on the pit

routing


How does core web api register a global routing method? The method to find code on the Internet is to add a feature before the method

I followed the online method and erased the following code after running and then it reported an error. It probably means that the controller must be modified by the RouteArrribute feature.

If it must be modified, then the global routing is meaningless, so write the routing for each controller.

And here's routing rule is not stated in advance

I got rid of the crossed code above, wrote the features as follows, and can still access normally according to the following routing rules

Swagger documentation


The API of the previous Framework version has the documentation plug-in core and the search is as follows   

swashbuckle.asonetcore 

 

Register the service after installation:

Code:

  public void ConfigureServices(IServiceCollection services)
        {
            services.AddSwaggerGen(s =>
            {
                s.SwaggerDoc("v1", new OpenApiInfo()
                {
                    Title = "test",
                    Version = "version-01",
                    Description = "好好学习"
                });
            }); 
            services.AddControllers();
        }

Then add it under Startup   

code show as below:

    //启用swagger
            app.UseSwagger();
            app.UseSwaggerUI(option=>{ option.SwaggerEndpoint("swagger/v1/swagger.json", "test1");});

Then debug it and see at the following address configuration is obviously wrong I swagger / v1 / swagger.json but the error is swagger / swagger / v1 / swagger.json I guess it is a swagger to provide their own root directory so I deleted a

    //启用swagger
            app.UseSwagger();
            app.UseSwaggerUI(option=>{ option.SwaggerEndpoint("v1/swagger.json", "test1");});

 

Sure enough, it was normal. I found that there are new problems here. 

It seems that the routing rules need to be changed. Later I found that this plug-in does not seem to know {} but knows []

 [Route("api/[Controller]/[Action]")]

The final words are perfect with an identity verification

Guess you like

Origin blog.csdn.net/qq_36445227/article/details/108981578