1+ using System . Diagnostics . CodeAnalysis ;
2+ using System . Text . Json . Serialization ;
3+ using Initium . Infrastructure . Results ;
4+ using Initium . Infrastructure . Transformers ;
5+ using Microsoft . Extensions . Hosting ;
6+ using Microsoft . AspNetCore . Mvc ;
7+ using Microsoft . AspNetCore . Mvc . ApplicationModels ;
8+ using Microsoft . AspNetCore . Routing ;
9+ using Microsoft . Extensions . DependencyInjection ;
10+
11+ namespace Initium . Extensions ;
12+
13+ [ SuppressMessage ( "ReSharper" , "UnusedMethodReturnValue.Global" ) ]
14+ public static class ServiceCollectionExtensions
15+ {
16+ /// <summary>
17+ /// Registers and configures Initium framework services and conventions.
18+ /// Applies API conventions, route slugification, lowercase URL enforcement,
19+ /// JSON serialization customization, exception behavior for background services,
20+ /// and a standardized invalid model state response.
21+ /// This method is designed to augment existing MVC configurations without enforcing specific pipeline usage.
22+ /// </summary>
23+ public static IServiceCollection AddInitium ( this IServiceCollection services )
24+ {
25+ services . Configure < MvcOptions > ( options =>
26+ options . Conventions . Add ( new RouteTokenTransformerConvention ( new SlugifyParameterTransformer ( ) ) ) ) ;
27+
28+ services . Configure < RouteOptions > ( options => options . LowercaseUrls = true ) ;
29+ services . Configure < HostOptions > ( options => options . BackgroundServiceExceptionBehavior = BackgroundServiceExceptionBehavior . Ignore ) ;
30+
31+ services . Configure < JsonOptions > ( options =>
32+ {
33+ options . JsonSerializerOptions . WriteIndented = true ;
34+ options . JsonSerializerOptions . Converters . Add ( new JsonStringEnumConverter ( ) ) ;
35+ } ) ;
36+
37+ services . PostConfigure < ApiBehaviorOptions > ( behaviorOptions =>
38+ behaviorOptions . InvalidModelStateResponseFactory = context => new InvalidModelStateResult ( context ) ) ;
39+
40+ return services ;
41+ }
42+ }
0 commit comments