Example #1
0
        /// <summary>
        /// Configures built-in .NET Core MVC (things like middleware, routing). Most of this configuration can be adjusted for the developers' need.
        /// Before calling .AddJsonApi(), a developer can register their own implementation of the following services to customize startup:
        /// <see cref="IResourceGraphBuilder"/>, <see cref="IServiceDiscoveryFacade"/>, <see cref="IJsonApiExceptionFilterProvider"/>,
        /// <see cref="IJsonApiTypeMatchFilterProvider"/> and <see cref="IJsonApiRoutingConvention"/>.
        /// </summary>
        public void ConfigureMvc(Type dbContextType)
        {
            RegisterJsonApiStartupServices();

            var intermediateProvider = _services.BuildServiceProvider();

            _resourceGraphBuilder   = intermediateProvider.GetRequiredService <IResourceGraphBuilder>();
            _serviceDiscoveryFacade = intermediateProvider.GetRequiredService <IServiceDiscoveryFacade>();
            _dbContextType          = dbContextType;

            AddResourceTypesFromDbContext(intermediateProvider);

            var exceptionFilterProvider = intermediateProvider.GetRequiredService <IJsonApiExceptionFilterProvider>();
            var typeMatchFilterProvider = intermediateProvider.GetRequiredService <IJsonApiTypeMatchFilterProvider>();
            var routingConvention       = intermediateProvider.GetRequiredService <IJsonApiRoutingConvention>();

            _mvcBuilder.AddMvcOptions(options =>
            {
                options.EnableEndpointRouting = true;
                options.Filters.Add(exceptionFilterProvider.Get());
                options.Filters.Add(typeMatchFilterProvider.Get());
                options.Filters.Add(new ConvertEmptyActionResultFilter());
                options.InputFormatters.Insert(0, new JsonApiInputFormatter());
                options.OutputFormatters.Insert(0, new JsonApiOutputFormatter());
                options.Conventions.Insert(0, routingConvention);
            });

            if (_options.ValidateModelState)
            {
                _mvcBuilder.AddDataAnnotations();
            }

            _services.AddSingleton <IControllerResourceMapping>(routingConvention);
        }
Example #2
0
        /// <summary>
        /// Configures built-in .net core MVC (things like middleware, routing). Most of this configuration can be adjusted for the developers need.
        /// Before calling .AddJsonApi(), a developer can register their own implementation of the following services to customize startup:
        /// <see cref="IResourceGraphBuilder"/>, <see cref="IServiceDiscoveryFacade"/>, <see cref="IJsonApiExceptionFilterProvider"/>,
        /// <see cref="IJsonApiTypeMatchFilterProvider"/>, <see cref="IJsonApiRoutingConvention"/> and <see cref="IResourceNameFormatter"/>.
        /// </summary>
        public void ConfigureMvc()
        {
            RegisterJsonApiStartupServices();

            var intermediateProvider = _services.BuildServiceProvider();

            _resourceGraphBuilder   = intermediateProvider.GetRequiredService <IResourceGraphBuilder>();
            _serviceDiscoveryFacade = intermediateProvider.GetRequiredService <IServiceDiscoveryFacade>();
            var exceptionFilterProvider = intermediateProvider.GetRequiredService <IJsonApiExceptionFilterProvider>();
            var typeMatchFilterProvider = intermediateProvider.GetRequiredService <IJsonApiTypeMatchFilterProvider>();
            var routingConvention       = intermediateProvider.GetRequiredService <IJsonApiRoutingConvention>();

            _mvcBuilder.AddMvcOptions(options =>
            {
                options.EnableEndpointRouting = true;
                options.Filters.Add(exceptionFilterProvider.Get());
                options.Filters.Add(typeMatchFilterProvider.Get());
                options.InputFormatters.Insert(0, new JsonApiInputFormatter());
                options.OutputFormatters.Insert(0, new JsonApiOutputFormatter());
                options.Conventions.Insert(0, routingConvention);
            });
            _services.AddSingleton <IControllerResourceMapping>(routingConvention);
        }