Exemple #1
0
        public static void AddCustomizedMvc(this IServiceCollection services, IConfiguration configuration)
        {
            var mvcBuilder = services
                             .AddControllers(options =>
            {
                //var defaultPolicy = new AuthorizationPolicyBuilder(AuthenticationConfig.GetAuthenticationSchemes(configuration))
                //	.RequireAuthenticatedUser()
                //	.Build();
                //options.Filters.Add(new AuthorizeFilter(defaultPolicy));
                options.Filters.Add(new ValidateModelAttribute {
                    ResultSelector = ValidationErrorModel.FromModelState()
                });
            })
                             .AddDataAnnotationsLocalization()
                             .ConfigureApiBehaviorOptions(options =>
            {
                //options.SuppressConsumesConstraintForFormFileParameters = true;
                //options.SuppressInferBindingSourcesForParameters = true;
                options.SuppressModelStateInvalidFilter = true;                         // zajišťujeme pomocí ValidateModelAttribute výše
            });

#if DEBUG
            mvcBuilder.AddJsonOptions(c => c.JsonSerializerOptions.WriteIndented = true);
#endif
        }
Exemple #2
0
 public static void AddCustomizedErrorToJson(this IServiceCollection services)
 {
     services.AddErrorToJson(c =>
     {
         c.Map(e => e is SecurityException, e => StatusCodes.Status403Forbidden, ValidationErrorModel.FromException(StatusCodes.Status403Forbidden), markExceptionAsHandled: e => true);
         c.Map(e => e is OperationFailedException, e => StatusCodes.Status422UnprocessableEntity, ValidationErrorModel.FromException(StatusCodes.Status422UnprocessableEntity), markExceptionAsHandled: e => true);
         c.Map(e => true /* ostatní výjimky */, e => StatusCodes.Status500InternalServerError, ValidationErrorModel.FromException(StatusCodes.Status500InternalServerError), markExceptionAsHandled: e => false);
     });
 }