Skip to content

Commit e414cd5

Browse files
committed
add controllers with views
1 parent 022c6e2 commit e414cd5

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

Directory.Build.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project>
22
<PropertyGroup>
33
<TargetFrameworks>net8.0</TargetFrameworks>
4-
<Version>1.0-rc.6</Version>
4+
<Version>1.0-rc.7</Version>
55
<TreatWarningsAsErrors>false</TreatWarningsAsErrors>
66
<Deterministic>true</Deterministic>
77
<Features>strict</Features>

src/features/Controllers/ControllersFeature.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,30 @@ public override void BeforeRun(WebApplication webBuilder)
3333
=> webBuilder.MapControllers();
3434
}
3535

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+
3660
[PublicAPI]
3761
public record CorsFeature(Action<CorsOptions> Configure) : AppFeature
3862
{
@@ -66,6 +90,8 @@ public static class ControllersFeatureIAppBuilder
6690
{
6791
public static IAppBuilder Controllers(this IAppBuilder builder, Action<MvcOptions>? config = null)
6892
=> builder.InjectFeature(() => new ControllersFeature(config));
93+
public static IAppBuilder ControllersWithViews(this IAppBuilder builder, Action<MvcOptions>? config = null)
94+
=> builder.InjectFeature(() => new ControllersWithViewsFeature(config));
6995
public static IAppBuilder Cors(this IAppBuilder builder, Action<CorsOptions> config)
7096
=> builder.InjectFeature(() => new CorsFeature(config));
7197
}

0 commit comments

Comments
 (0)