Last active
May 2, 2023 11:40
-
-
Save crgrieve/dc5e879d4c60417603e3d67066fb3cc4 to your computer and use it in GitHub Desktop.
Umbraco v9 startup.cs with Swagger
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using Microsoft.AspNetCore.Builder; | |
using Microsoft.AspNetCore.Hosting; | |
using Microsoft.Extensions.Configuration; | |
using Microsoft.Extensions.DependencyInjection; | |
using Microsoft.OpenApi.Models; | |
using Umbraco.Cms.Core.DependencyInjection; | |
using Umbraco.Extensions; | |
namespace Umbraco.Cms.Web.UI.NetCore | |
{ | |
public class Startup | |
{ | |
private readonly IWebHostEnvironment _env; | |
private readonly IConfiguration _config; | |
/// <summary> | |
/// Initializes a new instance of the <see cref="Startup"/> class. | |
/// </summary> | |
/// <param name="webHostEnvironment">The Web Host Environment</param> | |
/// <param name="config">The Configuration</param> | |
/// <remarks> | |
/// Only a few services are possible to be injected here https://github.com/dotnet/aspnetcore/issues/9337 | |
/// </remarks> | |
public Startup(IWebHostEnvironment webHostEnvironment, IConfiguration config) | |
{ | |
_env = webHostEnvironment ?? throw new ArgumentNullException(nameof(webHostEnvironment)); | |
_config = config ?? throw new ArgumentNullException(nameof(config)); | |
} | |
/// <summary> | |
/// Configures the services | |
/// </summary> | |
/// <remarks> | |
/// This method gets called by the runtime. Use this method to add services to the container. | |
/// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940 | |
/// </remarks> | |
public void ConfigureServices(IServiceCollection services) | |
{ | |
services.AddSwaggerGen(c => | |
{ | |
c.SwaggerDoc("v1", new OpenApiInfo { Title = "Carole Umbraco APIs", Version = "v1" }); | |
}); | |
services.AddUmbraco(_env, _config) | |
.AddBackOffice() | |
.AddWebsite() | |
.AddComposers() | |
.Build(); | |
} | |
/// <summary> | |
/// Configures the application | |
/// </summary> | |
public void Configure(IApplicationBuilder app) | |
{ | |
app.UseUmbracoBackOffice(); | |
app.UseUmbracoWebsite(); | |
app.UseSwagger(); | |
app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "Carole Umbraco APIs v1")); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment