Skip to content

Commit d52bc71

Browse files
lonix1khellang
authored andcommitted
Added AddProblemDetailsConventions overload (#1)
1 parent f98ddf0 commit d52bc71

File tree

1 file changed

+19
-4
lines changed

1 file changed

+19
-4
lines changed

src/ProblemDetails/Mvc/MvcBuilderExtensions.cs

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,33 @@ public static class MvcBuilderExtensions
1818
/// <param name="builder">The <see cref="IMvcBuilder"/>.</param>
1919
/// <returns>The <see cref="IMvcBuilder"/>.</returns>
2020
public static IMvcBuilder AddProblemDetailsConventions(this IMvcBuilder builder)
21+
{
22+
AddProblemDetailsConventions(builder.Services);
23+
24+
return builder;
25+
}
26+
27+
/// <summary>
28+
/// Adds conventions to turn off MVC's built-in <see cref="ApiBehaviorOptions.ClientErrorMapping"/>,
29+
/// adds a <see cref="ProducesErrorResponseTypeAttribute"/> to all actions with in controllers with an
30+
/// <see cref="ApiControllerAttribute"/> and a result filter that transforms <see cref="ObjectResult"/>
31+
/// containing a <see cref="string"/> to <see cref="ProblemDetails"/> responses.
32+
/// </summary>
33+
/// <param name="services">The <see cref="IServiceCollection"/>.</param>
34+
/// <returns>The <see cref="IServiceCollection"/>.</returns>
35+
public static IServiceCollection AddProblemDetailsConventions(this IServiceCollection services)
2136
{
2237
// Forward the MVC problem details factory registration to the factory used by the middleware.
23-
builder.Services.Replace(
38+
services.Replace(
2439
ServiceDescriptor.Singleton<MvcProblemDetailsFactory>(p => p.GetRequiredService<ProblemDetailsFactory>()));
2540

26-
builder.Services.TryAddEnumerable(
41+
services.TryAddEnumerable(
2742
ServiceDescriptor.Transient<IConfigureOptions<ApiBehaviorOptions>, ProblemDetailsApiBehaviorOptionsSetup>());
2843

29-
builder.Services.TryAddEnumerable(
44+
services.TryAddEnumerable(
3045
ServiceDescriptor.Transient<IApplicationModelProvider, ProblemDetailsApplicationModelProvider>());
3146

32-
return builder;
47+
return services;
3348
}
3449
}
3550
}

0 commit comments

Comments
 (0)