/// <summary> /// Adds the Hypermedia Extensions. /// By default a Siren Formatters is added and the entry assembly is crawled for Hypermedia route attributes /// </summary> public static IServiceCollection AddHypermediaExtensions(this IServiceCollection serviceCollection, Action <HypermediaExtensionsOptions> configureHypermediaOptionsAction = null) { var hypermediaOptions = new HypermediaExtensionsOptions(); configureHypermediaOptionsAction?.Invoke(hypermediaOptions); serviceCollection.TryAddSingleton <IActionContextAccessor, ActionContextAccessor>(); serviceCollection.AddSingleton(hypermediaOptions); serviceCollection.AddSingleton <IHypermediaUrlConfig>(hypermediaOptions.HypermediaUrlConfig); serviceCollection.AddSingleton(CreateApplicationModel); serviceCollection.AddSingletonWithAlternative <IRouteRegister, AttributedRoutesRegister>(hypermediaOptions.AlternateRouteRegister); serviceCollection.AddSingletonWithAlternative <IQueryStringBuilder, QueryStringBuilder>(hypermediaOptions.AlternateQueryStringBuilder); serviceCollection.AddSingleton <IRouteResolverFactory, RegisterRouteResolverFactory>(); serviceCollection.AddSingleton <IRouteKeyFactory, RouteKeyFactory>(); serviceCollection.AddSingleton <ISirenHypermediaConverterFactory, SirenHypermediaConverterFactory>(); serviceCollection.AddSingleton <HypermediaQueryLocationFormatter>(); serviceCollection.AddSingleton <HypermediaEntityLocationFormatter>(); serviceCollection.AddSingleton <SirenHypermediaFormatter>(); if (hypermediaOptions.AutoDeliverJsonSchemaForActionParameterTypes) { serviceCollection.AddSingleton <ActionParameterSchemas>(); } serviceCollection.ConfigureOptions <ConfigureMvcOptionsForHypermediaExtensions>(); return(serviceCollection); }
public ConfigureMvcOptionsForHypermediaExtensions( HypermediaExtensionsOptions hypermediaOptions, ApplicationModel applicationModel, HypermediaQueryLocationFormatter hypermediaQueryLocationFormatter, HypermediaEntityLocationFormatter hypermediaEntityLocationFormatter, SirenHypermediaFormatter sirenHypermediaFormatter) { this.hypermediaOptions = hypermediaOptions; this.applicationModel = applicationModel; this.hypermediaQueryLocationFormatter = hypermediaQueryLocationFormatter; this.hypermediaEntityLocationFormatter = hypermediaEntityLocationFormatter; this.sirenHypermediaFormatter = sirenHypermediaFormatter; }
/// <summary> /// Add custom binder for parameters of hypermedia actions that derive from <see cref="IHypermediaActionParameter"/>. /// Enables usage of <see cref="KeyFromUriAttribute"/> for properties of those parameter types. /// </summary> /// <param name="options"></param> /// <param name="hypermediaOptions"></param> /// <param name="applicationModel">Model of the HTO objects and controllers</param> /// <returns></returns> public static MvcOptions AddHypermediaParameterBinders(this MvcOptions options, HypermediaExtensionsOptions hypermediaOptions, ApplicationModel applicationModel) { var forAttributedActionParametersOnly = !hypermediaOptions.ImplicitHypermediaActionParameterBinders; options.ModelBinderProviders.Insert(0, new HypermediaParameterFromBodyBinderProvider(t => { if (!applicationModel.HmoTypes.TryGetValue(t, out var hmoType)) { throw new ArgumentException($"No route found for type {t.BeautifulName()}"); } return(hmoType.GetHmoMethods.Select(_ => _.RouteTemplateFull).ToImmutableArray()); }, forAttributedActionParametersOnly)); return(options); }