@@ -33,6 +33,30 @@ public override void BeforeRun(WebApplication webBuilder)
33
33
=> webBuilder . MapControllers ( ) ;
34
34
}
35
35
36
+ [ PublicAPI ]
37
+ public record ControllersWithViewsFeature ( Action < MvcOptions > ? Configure ) : AppFeature
38
+ {
39
+ public override void BeforeRegistrations ( WebApplicationBuilder webBuilder )
40
+ {
41
+ webBuilder . Services . AddTransient ( s =>
42
+ s . GetRequiredService < IHttpContextAccessor > ( ) . HttpContext ! . User ) ;
43
+ webBuilder . Services . AddHttpContextAccessor ( ) ;
44
+ webBuilder . Services
45
+ . AddControllersWithViews ( this . Configure )
46
+ . ConfigureApiBehaviorOptions ( options => options . InvalidModelStateResponseFactory = context =>
47
+ new BadRequestObjectResult ( context . ModelState )
48
+ {
49
+ ContentTypes = { System . Net . Mime . MediaTypeNames . Application . Json }
50
+ } )
51
+ . AddJson ( )
52
+ . AddEndpointsApiExplorer ( ) ;
53
+ }
54
+
55
+ public override void BeforeRun ( WebApplication webBuilder )
56
+ => webBuilder . MapControllers ( ) ;
57
+ }
58
+
59
+
36
60
[ PublicAPI ]
37
61
public record CorsFeature ( Action < CorsOptions > Configure ) : AppFeature
38
62
{
@@ -66,6 +90,8 @@ public static class ControllersFeatureIAppBuilder
66
90
{
67
91
public static IAppBuilder Controllers ( this IAppBuilder builder , Action < MvcOptions > ? config = null )
68
92
=> builder . InjectFeature ( ( ) => new ControllersFeature ( config ) ) ;
93
+ public static IAppBuilder ControllersWithViews ( this IAppBuilder builder , Action < MvcOptions > ? config = null )
94
+ => builder . InjectFeature ( ( ) => new ControllersWithViewsFeature ( config ) ) ;
69
95
public static IAppBuilder Cors ( this IAppBuilder builder , Action < CorsOptions > config )
70
96
=> builder . InjectFeature ( ( ) => new CorsFeature ( config ) ) ;
71
97
}
0 commit comments