示例#1
0
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseSwagger(options =>
            {
                options.RouteTemplate = "docs/{documentName}/swagger.json";
            });
            app.UseSwaggerUI(options =>
            {
                options.RoutePrefix = "docs";
                options.SwaggerEndpoint("/docs/v1/swagger.json", "TesteT2S.WebApi v1");
            });

            //app.UseHttpsRedirection();

            app.UseRouting();

            ApplicationCorsOptions corsOptions = app.ApplicationServices
                                                 .GetService <IOptions <ApplicationCorsOptions> >().Value;

            app.UseCors(corsOptions.PolicyName);

            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });
        }
示例#2
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services
            .AddControllers()
            .AddFluentValidation(fluentValidations => fluentValidations
                                 .RegisterValidatorsFromAssemblyContaining <Startup>());;
            services.AddSwaggerGen(options =>
            {
                options.SwaggerDoc("v1", new OpenApiInfo
                {
                    Title   = "TesteT2S.WebApi",
                    Version = "v1"
                });
                string xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
                string xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
                options.IncludeXmlComments(xmlPath);
                options.AddFluentValidationRules();
            });

            string connectionString = Configuration.GetConnectionString("SqlServerConnection");

            services.AddDbContext <ContainerContext>(options =>
            {
                options.UseSqlServer(connectionString);
                options.LogTo(Console.WriteLine);
            });
            services.AddTransient <IDbConnection>(servicesProvides => new SqlConnection(connectionString));
            services.AddScoped <IReportRepository, ReportRepository>();

            services.AddAutoMapper(typeof(ContainerProfile));

            var corsOptions = new ApplicationCorsOptions();

            Configuration.Bind("CorsOptions", corsOptions);
            services
            .Configure <ApplicationCorsOptions>(Configuration.GetSection("CorsOptions"))
            .AddCors(options =>
            {
                options.AddPolicy(corsOptions.PolicyName, configurePolicy =>
                {
                    configurePolicy
                    .AllowCredentials()
                    .AllowAnyHeader()
                    .AllowAnyMethod()
                    .WithOrigins(corsOptions.AllowedOrigin);
                });
            });
        }
示例#3
0
        public static IServiceCollection AddApplicationCorsPolicy(this IServiceCollection services, IConfiguration configuration)
        {
            var corsOptions = new ApplicationCorsOptions();

            configuration.Bind("CorsOptions", corsOptions);
            return(services
                   .Configure <ApplicationCorsOptions>(configuration.GetSection("CorsOptions"))
                   .AddCors(options =>
            {
                options.AddPolicy(corsOptions.PolicyName, configurePolicy =>
                {
                    configurePolicy
                    .AllowCredentials()
                    .AllowAnyHeader()
                    .AllowAnyMethod()
                    .WithOrigins(corsOptions.AllowedOrigin);
                });
            }));
        }
示例#4
0
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseStaticFiles(new StaticFileOptions
            {
                ServeUnknownFileTypes = true,
                DefaultContentType    = "text/plain"
            });

            app.UseRouting();

            ApplicationCorsOptions corsOptions = app.ApplicationServices
                                                 .GetService <IOptions <ApplicationCorsOptions> >().Value;

            app.UseCors(corsOptions.PolicyName);

            app.UseAuthentication();
            app.UseAuthorization();

            app.UseSwaggerUi3(settings =>
            {
                settings.Path         = "/docs";
                settings.DocumentPath = "/docs/v1/openapi.yaml";
            });

            app.UseMiddleware <ValidationErrorMiddleware>();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });
        }