public void IsNotQueryModelBinder() { var modelBinderProviderContextMock = new ModelBinderProviderContextMock(typeof(string)); var expected = new QueryModelBinderProvider(new AutumnSettings()); Assert.Null(expected.GetBinder(modelBinderProviderContextMock)); }
public void IsQueryModelBinder() { var modelBinderProviderContextMock = new ModelBinderProviderContextMock(typeof(Expression <Func <string, bool> >)); var expected = new QueryModelBinderProvider(new AutumnSettings()); Assert.NotNull(expected.GetBinder(modelBinderProviderContextMock)); }
/// <summary> /// Adds <see cref="WebOptions"/> and services to the <see cref="IServiceCollection"/>. /// </summary> /// <param name="services">The <see cref="IServiceCollection"/>.</param> /// <param name="configuration">The <see cref="IConfiguration"/>.</param> /// <returns>The <see cref="IServiceCollection"/>.</returns> internal static IServiceCollection AddWeb(this IServiceCollection services, IConfiguration configuration) { if (services == null) { throw new ArgumentNullException(nameof(services)); } if (configuration == null) { throw new ArgumentNullException(nameof(configuration)); } var assembly = typeof(BaseController).GetTypeInfo().Assembly; services .AddConfigOptions <WebOptions>(configuration, WebOptions.SectionName, out var webOptions); var serviceProvider = services .BuildServiceProvider(); var appOptions = serviceProvider .GetService <AppOptions>() ?? new AppOptions(); var dataOptions = serviceProvider .GetService <DataOptions>() ?? new DataOptions(); var securityOptions = serviceProvider .GetService <SecurityOptions>() ?? new SecurityOptions(); services .AddCors() .AddSession() .AddCaching(webOptions) .AddSecurity(securityOptions) .AddRepository() .AddVersioning(appOptions) .AddDocumentation(appOptions, webOptions) .AddLocalizations() .AddTimeZone(appOptions) .AddCompression() .AddContentTypeFormatters() .Configure <ForwardedHeadersOptions>(x => { x.ForwardedHeaders = ForwardedHeaders.All; }) .AddSingleton <ExceptionHandlingMiddleware>() .AddSingleton <HttpRequestOptionsMiddleware>() .AddSingleton <HttpRequestIdentifierMiddleware>() .AddSingleton <IHttpContextAccessor, HttpContextAccessor>() .AddRouting() .AddMvc(x => { var routeAttribute = new RouteAttribute(webOptions.Hosting.Root); var routePrefixConvention = new RoutePrefixConvention(routeAttribute); var queryModelBinderProvider = new QueryModelBinderProvider(); x.Conventions.Insert(0, routePrefixConvention); x.ModelBinderProviders.Insert(0, queryModelBinderProvider); if (!securityOptions.IsAuth || dataOptions.ConnectionString == null) { x.Conventions.Insert(1, new AuthControllerDisabledConvention()); } if (dataOptions.ConnectionString == null || !dataOptions.UseAudit) { x.Conventions.Insert(2, new AuditControllerDisabledConvention()); } if (webOptions.Hosting.UseHttpsRequired) { x.Filters.Add <RequireHttpsAttribute>(); } x.Filters.Add <IsAnonymousFilter>(); x.Filters.Add <ModelStateValidationFilter>(); }) .AddNewtonsoftJson(x => { x.AllowInputFormatterExceptionMessages = true; x.SerializerSettings.Culture = CultureInfo.CurrentCulture; x.SerializerSettings.NullValueHandling = NullValueHandling.Ignore; x.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore; x.SerializerSettings.PreserveReferencesHandling = PreserveReferencesHandling.None; x.SerializerSettings.ContractResolver = new EntityContractResolver(); x.SerializerSettings.Converters .Add(new StringEnumConverter()); }) .AddControllersAsServices() .AddViewComponentsAsServices() .AddApplicationPart(assembly); services .AddScoped <AuditController>(); services .AddApis() .AddHealthChecking(appOptions, webOptions); return(services); }
/// <summary> /// Adds <see cref="WebOptions"/> and services to the <see cref="IServiceCollection"/>. /// </summary> /// <param name="services">The <see cref="IServiceCollection"/>.</param> /// <param name="configuration">The <see cref="IConfiguration"/>.</param> /// <returns>The <see cref="IServiceCollection"/>.</returns> internal static IServiceCollection AddWeb(this IServiceCollection services, IConfiguration configuration) { if (services == null) { throw new ArgumentNullException(nameof(services)); } if (configuration == null) { throw new ArgumentNullException(nameof(configuration)); } var assembly = typeof(BaseController).GetTypeInfo().Assembly; services .AddConfigOptions <WebOptions>(configuration, WebOptions.SectionName, out var options); var serviceProvider = services.BuildServiceProvider(); var dataOptions = serviceProvider.GetService <DataOptions>(); var securityOptions = serviceProvider.GetService <SecurityOptions>(); services .AddCors() .AddSession() .AddSecurity() .AddRepository() .AddVersioning() .AddDocumentation() .AddLocalizations() .AddCompression() .AddContentTypeFormatters() .AddSingleton <ExceptionHandlingMiddleware>() .AddSingleton <HttpRequestOptionsMiddleware>() .AddSingleton <HttpRequestIdentifierMiddleware>() .AddSingleton <IHttpContextAccessor, HttpContextAccessor>() .AddMvc(x => { var routeAttribute = new RouteAttribute(options.Hosting.Root); var routePrefixConvention = new RoutePrefixConvention(routeAttribute); var queryModelBinderProvider = new QueryModelBinderProvider(); x.Conventions.Insert(0, routePrefixConvention); x.ModelBinderProviders.Insert(0, queryModelBinderProvider); if (dataOptions.ConnectionString == null) { x.Conventions.Insert(1, new AduitControllerDisabledConvention()); } if (dataOptions.ConnectionString == null || !securityOptions.IsEnabled) { x.Conventions.Insert(0, new AuthControllerDisabledConvention()); } if (options.Hosting.UseHttpsRequired) { x.Filters.Add <RequireHttpsAttribute>(); } x.Filters.Add <ModelStateValidationFilter>(); x.Filters.Add <DisableLazyLoadingResultFilterAttribute>(); }) .AddJsonOptions(x => { x.AllowInputFormatterExceptionMessages = true; x.SerializerSettings.Culture = CultureInfo.CurrentCulture; x.SerializerSettings.NullValueHandling = NullValueHandling.Ignore; x.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore; x.SerializerSettings.PreserveReferencesHandling = PreserveReferencesHandling.None; x.SerializerSettings.ContractResolver = new EntityContractResolver(); x.SerializerSettings.Converters .Add(new StringEnumConverter()); }) .AddControllersAsServices() .AddViewComponentsAsServices() .AddApplicationPart(assembly) .SetCompatibilityVersion(options.CompatabilityVersion); services .Configure <RazorViewEngineOptions>(x => { x.FileProviders .Add(new EmbeddedFileProvider(assembly)); }); services .AddApis() .AddStartupTasks() .AddHealthChecking(options); return(services); }