All Downloads are FREE. Search and download functionalities are using the official Maven repository.

aspnetcore.2.1.Startup.mustache Maven / Gradle / Ivy

There is a newer version: 7.6.0
Show newest version
{{>partial_header}}
using System;
using System.IO;
using System.Reflection;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Serialization;{{#useSwashbuckle}}
using Swashbuckle.AspNetCore.Swagger;
using Swashbuckle.AspNetCore.SwaggerGen;
using {{packageName}}.Filters;{{/useSwashbuckle}}
using {{packageName}}.Authentication;
using {{packageName}}.Formatters;
using Microsoft.AspNetCore.Authorization;

namespace {{packageName}}
{
    /// 
    /// Startup
    /// 
    public class Startup
    {
        /// 
        /// Constructor
        /// 
        /// 
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        /// 
            /// The application configuration.
        /// 
        public IConfiguration Configuration { get; }

        /// 
        /// This method gets called by the runtime. Use this method to add services to the container.
        /// 
        /// 
        public void ConfigureServices(IServiceCollection services)
        {
{{#authMethods}}
{{#isApiKey}}
            services.AddTransient();
            services.AddAuthorization(authConfig =>
            {
                authConfig.AddPolicy("{{name}}",
                    policyBuilder => policyBuilder
                        .AddRequirements(new ApiKeyRequirement(new[] { "my-secret-key" },"{{name}}")));
            });
{{/isApiKey}}
{{/authMethods}}

            // Add framework services.
            services
                .AddMvc(opts => {
                    {{^useDefaultRouting}}opts.EnableEndpointRouting = false;{{/useDefaultRouting}}
                    opts.InputFormatters.Insert(0, new InputFormatterStream());
                    })
                {{#compatibilityVersion}}
                .SetCompatibilityVersion(CompatibilityVersion.{{.}})
                {{/compatibilityVersion}}
                .{{#useNewtonsoft}}AddNewtonsoftJson{{/useNewtonsoft}}{{^useNewtonsoft}}AddJsonOptions{{/useNewtonsoft}}(opts =>
                {
                    opts.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
                    opts.SerializerSettings.Converters.Add(new StringEnumConverter
                    {
                        {{#useNewtonsoft}}NamingStrategy = new CamelCaseNamingStrategy(){{/useNewtonsoft}}{{^useNewtonsoft}}CamelCaseText = true{{/useNewtonsoft}}
                    });
                });
            {{#useSwashbuckle}}

            services
                .AddSwaggerGen(c =>
                {
                    c.SwaggerDoc("{{{version}}}{{^version}}v1{{/version}}", new Info
                    {
                        Version = "{{{version}}}{{^version}}v1{{/version}}",
                        Title = "{{{appName}}}{{^appName}}{{packageName}}{{/appName}}",
                        Description = "{{{appName}}}{{^appName}}{{packageName}}{{/appName}} (ASP.NET Core {{aspnetCoreVersion}})",
                        Contact = new Contact()
                        {
                           Name = "{{{infoName}}}{{^infoName}}OpenAPI-Generator Contributors{{/infoName}}",
                           Url = "{{{infoUrl}}}{{^infoUrl}}https://github.com/openapitools/openapi-generator{{/infoUrl}}",
                           Email = "{{{infoEmail}}}"
                        },
                        TermsOfService = "{{{termsOfService}}}"
                    });
                    c.CustomSchemaIds(type => type.FriendlyId(true));
                    c.DescribeAllEnumsAsStrings();
                    c.IncludeXmlComments($"{AppContext.BaseDirectory}{Path.DirectorySeparatorChar}{Assembly.GetEntryAssembly().GetName().Name}.xml");
                    {{#basePathWithoutHost}}
                    // Sets the basePath property in the Swagger document generated
                    c.DocumentFilter("{{{.}}}");
                    {{/basePathWithoutHost}}

                    // Include DataAnnotation attributes on Controller Action parameters as Swagger validation rules (e.g required, pattern, ..)
                    // Use [ValidateModelState] on Actions to actually validate it in C# as well!
                    c.OperationFilter();
                });
            {{/useSwashbuckle}}
        }

        /// 
        /// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        /// 
        /// 
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            app.UseHttpsRedirection();
            app
                .UseMvc()
                .UseDefaultFiles()
                .UseStaticFiles(){{#useSwashbuckle}}
                .UseSwagger(c =>
                {
                    c.RouteTemplate = "swagger/{documentName}/openapi.json";
                })
                .UseSwaggerUI(c =>
                {
                    //TODO: Either use the SwaggerGen generated Swagger contract (generated from C# classes)
                    c.SwaggerEndpoint("/swagger/{{{version}}}{{^version}}v1{{/version}}/openapi.json", "{{{appName}}}{{^appName}}{{packageName}}{{/appName}}");

                    //TODO: Or alternatively use the original Swagger contract that's included in the static files
                    // c.SwaggerEndpoint("/openapi-original.json", "{{{appName}}}{{^appName}}{{packageName}}{{/appName}} Original");
                }){{/useSwashbuckle}};
            {{^useDefaultRouting}}
            app.UseRouting();
            app.UseEndpoints(endpoints =>
                {
                    endpoints.MapControllers();
                });
            {{/useDefaultRouting}}

            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseHsts();
            }
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy