Example #1
0
        /// <summary>
        /// Add Halcyon with some custom formatters that handle marked up models better.
        /// </summary>
        /// <param name="mvcOptions">The MVC options to extend.</param>
        /// <param name="options">The hal options, send the same value you sent to AddConventionalHalcyon.</param>
        /// <returns></returns>
        public static MvcOptions UseConventionalHalcyon(this MvcOptions mvcOptions, HalcyonConventionOptions options)
        {
            mvcOptions.RespectBrowserAcceptHeader = true;
            var mediaTypes      = new string[] { ProducesHalAttribute.MediaType };
            var outputFormatter = new JsonHalOutputFormatter(options.JsonSerializerSettings
#if NET6_0
                                                             , mvcOptions
#endif
                                                             , mediaTypes);

            mvcOptions.OutputFormatters.Add(outputFormatter);
            if (options.MakeAllControllersHalcyon)
            {
                mvcOptions.Filters.Add(new ProducesHalAttribute());
                mvcOptions.Filters.AddService(typeof(HalModelResultFilterAttribute));
            }
            mvcOptions.Conventions.Add(new ApiExplorerVisibilityEnabledConvention());

            return(mvcOptions);
        }
Example #2
0
        /// <summary>
        /// Add the conventional Halcyon services. This will set everything up for attributes to work correctly.
        /// You must use this function when calling UseConventionalHalcyon on MvcOptions or the services will
        /// not be setup correctly.
        /// </summary>
        /// <param name="services">The service collection to modify.</param>
        /// <param name="options">The options.</param>
        /// <returns></returns>
        public static IServiceCollection AddConventionalHalcyon(this IServiceCollection services, HalcyonConventionOptions options)
        {
            services.TryAddSingleton <ILabelCacheFactory, LabelCacheFactory>();
            services.TryAddSingleton <IActionContextAccessor, ActionContextAccessor>();
            services.TryAddSingleton <IUrlHelperFactory, UrlHelperFactory>();
            services.TryAddSingleton <HalcyonConventionOptions>(options);
            services.TryAddScoped <IHALConverter, CustomHalAttributeConverter>();
            services.TryAddScoped <HalModelResultFilterAttribute, HalModelResultFilterAttribute>();
            services.TryAddSingleton <IHttpContextAccessor, HttpContextAccessor>();
            services.TryAddScoped <IEndpointDocBuilder, EndpointDocBuilder>();
            services.TryAddSingleton <IEndpointDocCache, EndpointDocCache>();
            services.TryAddScoped <IValidSchemaTypeManager, ValidSchemaTypeManager>();
            services.TryAddScoped(typeof(PropertyNameValueProvider <>));
            services.TryAddScoped <IValueProviderResolver>(s =>
            {
                return(new ValueProviderResolver(s));
            });
            services.TryAddScoped <ISchemaCustomizerResolver>(s =>
            {
                return(new SchemaCustomizerResolver(s));
            });
            services.TryAddScoped <ISchemaBuilder>(s =>
            {
                var generator = new EndpointDocJsonSchemaGenerator(options.JsonSchemaGeneratorSettings, s.GetRequiredService <IValueProviderResolver>(), s.GetRequiredService <ISchemaCustomizerResolver>(), s.GetRequiredService <IAutoTitleGenerator>());
                generator.UseValueProviders = options.EnableValueProviders;
                return(new SchemaBuilder(generator, s.GetRequiredService <IValidSchemaTypeManager>()));
            });
            services.TryAddSingleton <IAutoTitleGenerator, AutoTitleGenerator>();

            return(services);
        }