Exemple #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.UseExceptionHandler(errorApp =>
                {
                    errorApp.Run(async context =>
                    {
                        var errorFeature = context.Features.Get <IExceptionHandlerFeature>();
                        var exception    = errorFeature.Error;

                        var errorDetail    = exception.ToString();
                        var problemDetails = new ProblemDetails
                        {
                            Instance = $"urn:myorganization:error:{Guid.NewGuid()}"
                        };

                        if (exception is BadHttpRequestException badHttpRequestException)
                        {
                            problemDetails.Title  = "Invalid request";
                            problemDetails.Status = (int)typeof(BadHttpRequestException).GetProperty("StatusCode",
                                                                                                     BindingFlags.NonPublic | BindingFlags.Instance).GetValue(badHttpRequestException);
                            problemDetails.Detail = badHttpRequestException.Message;
                        }
                        else
                        {
                            problemDetails.Title  = "An unexpected error occurred!";
                            problemDetails.Status = 500;
                            problemDetails.Detail = exception.ToString();
                        }

                        // log the exception etc..

                        context.Response.StatusCode = problemDetails.Status.Value;
                        context.Response.WriteJson(problemDetails, "application/problem+json");
                    });
                });
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
                // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
                app.UseHsts();
            }
            app.UseHttpsRedirection();
            app.UseStaticFiles();

            app.UseRouting();

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

            app.UseSwagger();
            app.UseSwaggerUI(SwaggerConfig.SwaggerUIConfig);

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllerRoute(
                    name: "default",
                    pattern: "{controller=Home}/{action=Index}/{id?}");
            });
        }
Exemple #2
0
 public AnswerModel(ProblemDetails problemDetails)
 {
     ProblemDetails = problemDetails;
 }