Пример #1
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            IMvcBuilder mvcBuilder = null;

#if NETCOREAPP2_2
            mvcBuilder = services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
#endif
#if NETCOREAPP3_0
            mvcBuilder = services.AddControllers().SetCompatibilityVersion(CompatibilityVersion.Version_3_0);
#endif
            mvcBuilder.AddHttpExceptions(options =>
            {
#if NETCOREAPP2_2
                // This is the same as the default behavior; only include exception details in a development environment.
                options.IncludeExceptionDetails = context => context.RequestServices.GetRequiredService <IHostingEnvironment>().IsDevelopment();
#endif
#if NETCOREAPP3_0
                // This is the same as the default behavior; only include exception details in a development environment.
                options.IncludeExceptionDetails = context => context.RequestServices.GetRequiredService <IWebHostEnvironment>().EnvironmentName == Environments.Development;
#endif
                // This is a simplified version of the default behavior; only map exceptions for 4xx and 5xx responses.
                options.IsExceptionResponse = context => (context.Response.StatusCode >= 400 && context.Response.StatusCode < 600);

                // custom exception mapper does not map to Problem Details
                options.ExceptionMapper <FormatException, CustomExceptionMapper>();
                // default exception mapper for mapping to Problem Details
                options.ExceptionMapper <Exception, ProblemDetailsExceptionMapper <Exception> >();
            });

            // serializers for returning "application/xml"
            mvcBuilder.AddXmlSerializerFormatters().AddXmlDataContractSerializerFormatters();

            services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme).AddJwtBearer();
        }