Designbest Rest API

Da Webmobili Wiki.

Configurazione di Swagger

Nel file Program.cs configurare così

builder.Services.AddSwaggerGen(
	opt => {
		opt.SwaggerDoc("v1", new OpenApiInfo {
			Title = "Designbest REST API",
			Description = "Designbest REST API",
			Version = "v1",
			Contact = new OpenApiContact { Email = "info@designbest.com", Name = "Webmobili s.r.l." }
		});
		var tokenService = new TokenService(builder.Configuration, builder.Environment);
		opt.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme {
			In = ParameterLocation.Header,
			Description = "Please enter a valid token like " + tokenService.GetToken()?.AccessToken,
			Name = "Authorization",
			Type = SecuritySchemeType.Http,
			BearerFormat = "JWT",
			Scheme = "Bearer"
		});
		opt.AddSecurityRequirement(new OpenApiSecurityRequirement {
			{
				new OpenApiSecurityScheme {
					Reference = new OpenApiReference {
						Type=ReferenceType.SecurityScheme,
						Id=JwtBearerDefaults.AuthenticationScheme
					}
				},
				Array.Empty<string>()
			}
		});

		var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
		opt.IncludeXmlComments(Path.Combine(AppContext.BaseDirectory, xmlFile));
	}
);

e di sotto, per poter cambiare il nome dell'endpoint della documentazione in /designbest e personalizzare la grafica

app.UseSwagger(options => {
	options.RouteTemplate = "designbest/{documentName}/swagger.json";
});
app.UseSwaggerUI(options => {
	options.InjectStylesheet("/css/designbest-swagger.css");
	options.InjectJavascript("/js/swagger-custom.js");
	options.RoutePrefix = "designbest";
	options.SwaggerEndpoint("/designbest/v1/swagger.json", "Designbest REST API");
});
// ...
app.UseStaticFiles();
  • RouteTemplate, RoutePrefix e SwaggerEndpoint() sono collegati e servono a cambiare il nome default per raggiungere la url della documentazione
  • InjectStylesheet() e InjectJavascript() servono a personalizzare la grafica.
    Perché questi funzionino è necessario attivare app.UseStaticFiles(); e creare la cartella wwwroot che conterrà le cartelle css e js

Sotto Properties/launchSettings.json modificare i campi launchUrl con la stringa designbest.

{
  "$schema": "https://json.schemastore.org/launchsettings.json",
  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "http://localhost:41632",
      "sslPort": 44362
    }
  },
  "profiles": {
    "http": {
      "commandName": "Project",
      "dotnetRunMessages": true,
      "launchBrowser": true,
      "launchUrl": "designbest",
      "applicationUrl": "http://localhost:5247",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "https": {
      "commandName": "Project",
      "dotnetRunMessages": true,
      "launchBrowser": true,
      "launchUrl": "designbest",
      "applicationUrl": "https://localhost:7031;http://localhost:5247",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "launchUrl": "designbest",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }
  }
}