Designbest Rest API: differenze tra le versioni

Da Webmobili Wiki.
Riga 37: Riga 37:
</syntaxhighlight>
</syntaxhighlight>


e di sotto
e di sotto, per poter cambiare il nome dell'endpoint della documentazione e personalizzare la grafica
<syntaxhighlight lang="c#">
<syntaxhighlight lang="c#">
app.UseSwagger(options => {
app.UseSwagger(options => {
Riga 54: Riga 54:
* <code>RouteTemplate</code>, <code>RoutePrefix</code> e <code>SwaggerEndpoint()</code> sono collegati e servono a cambiare il nome default per raggiungere la url della documentazione
* <code>RouteTemplate</code>, <code>RoutePrefix</code> e <code>SwaggerEndpoint()</code> sono collegati e servono a cambiare il nome default per raggiungere la url della documentazione
* <p><code>InjectStylesheet()</code> e <code>InjectJavascript()</code> servono a personalizzare la grafica.<br/>Perché questi funzionino è necessario attivare <code>app.UseStaticFiles();</code> e creare la cartella <code>wwwroot</code> che conterrà le cartelle <code>css</code> e <code>js</code></p>
* <p><code>InjectStylesheet()</code> e <code>InjectJavascript()</code> servono a personalizzare la grafica.<br/>Perché questi funzionino è necessario attivare <code>app.UseStaticFiles();</code> e creare la cartella <code>wwwroot</code> che conterrà le cartelle <code>css</code> e <code>js</code></p>
Sotto <code>Properties/launchSettings.json</code> modificare i campi ''launchUrl'' con la stringa ''designbest''.
<syntaxhighlight lang="json">
{
  "$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"
      }
    }
  }
}
</syntaxhighlight>

Versione delle 17:48, 7 nov 2024

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 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"
      }
    }
  }
}