Skip to content

Commit

Permalink
add controllers with views
Browse files Browse the repository at this point in the history
  • Loading branch information
0xF6 committed Jan 18, 2024
1 parent 022c6e2 commit e414cd5
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project>
<PropertyGroup>
<TargetFrameworks>net8.0</TargetFrameworks>
<Version>1.0-rc.6</Version>
<Version>1.0-rc.7</Version>
<TreatWarningsAsErrors>false</TreatWarningsAsErrors>
<Deterministic>true</Deterministic>
<Features>strict</Features>
Expand Down
26 changes: 26 additions & 0 deletions src/features/Controllers/ControllersFeature.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,30 @@ public override void BeforeRun(WebApplication webBuilder)
=> webBuilder.MapControllers();
}

[PublicAPI]
public record ControllersWithViewsFeature(Action<MvcOptions>? Configure) : AppFeature
{
public override void BeforeRegistrations(WebApplicationBuilder webBuilder)
{
webBuilder.Services.AddTransient(s =>
s.GetRequiredService<IHttpContextAccessor>().HttpContext!.User);
webBuilder.Services.AddHttpContextAccessor();
webBuilder.Services
.AddControllersWithViews(this.Configure)
.ConfigureApiBehaviorOptions(options => options.InvalidModelStateResponseFactory = context =>
new BadRequestObjectResult(context.ModelState)
{
ContentTypes = { System.Net.Mime.MediaTypeNames.Application.Json }
})
.AddJson()
.AddEndpointsApiExplorer();
}

public override void BeforeRun(WebApplication webBuilder)
=> webBuilder.MapControllers();
}


[PublicAPI]
public record CorsFeature(Action<CorsOptions> Configure) : AppFeature
{
Expand Down Expand Up @@ -66,6 +90,8 @@ public static class ControllersFeatureIAppBuilder
{
public static IAppBuilder Controllers(this IAppBuilder builder, Action<MvcOptions>? config = null)
=> builder.InjectFeature(() => new ControllersFeature(config));
public static IAppBuilder ControllersWithViews(this IAppBuilder builder, Action<MvcOptions>? config = null)
=> builder.InjectFeature(() => new ControllersWithViewsFeature(config));
public static IAppBuilder Cors(this IAppBuilder builder, Action<CorsOptions> config)
=> builder.InjectFeature(() => new CorsFeature(config));
}

0 comments on commit e414cd5

Please sign in to comment.