Provide a convenience API to configure ApiBehaviorOptions
This commit is contained in:
parent
40959a97e7
commit
2544926b2f
|
|
@ -151,5 +151,30 @@ namespace Microsoft.Extensions.DependencyInjection
|
||||||
builder.Services.Configure<MvcCompatibilityOptions>(o => o.CompatibilityVersion = version);
|
builder.Services.Configure<MvcCompatibilityOptions>(o => o.CompatibilityVersion = version);
|
||||||
return builder;
|
return builder;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Configures <see cref="ApiBehaviorOptions"/>.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="builder">The <see cref="IMvcBuilder"/>.</param>
|
||||||
|
/// <param name="setupAction">The configure action.</param>
|
||||||
|
/// <returns>The <see cref="IMvcBuilder"/>.</returns>
|
||||||
|
public static IMvcBuilder ConfigureApiBehaviorOptions(
|
||||||
|
this IMvcBuilder builder,
|
||||||
|
Action<ApiBehaviorOptions> setupAction)
|
||||||
|
{
|
||||||
|
if (builder == null)
|
||||||
|
{
|
||||||
|
throw new ArgumentNullException(nameof(builder));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (setupAction == null)
|
||||||
|
{
|
||||||
|
throw new ArgumentNullException(nameof(setupAction));
|
||||||
|
}
|
||||||
|
|
||||||
|
builder.Services.Configure(setupAction);
|
||||||
|
|
||||||
|
return builder;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -169,6 +169,31 @@ namespace Microsoft.Extensions.DependencyInjection
|
||||||
return builder;
|
return builder;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Configures <see cref="ApiBehaviorOptions"/>.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="builder">The <see cref="IMvcCoreBuilder"/>.</param>
|
||||||
|
/// <param name="setupAction">The configure action.</param>
|
||||||
|
/// <returns>The <see cref="IMvcCoreBuilder"/>.</returns>
|
||||||
|
public static IMvcCoreBuilder ConfigureApiBehaviorOptions(
|
||||||
|
this IMvcCoreBuilder builder,
|
||||||
|
Action<ApiBehaviorOptions> setupAction)
|
||||||
|
{
|
||||||
|
if (builder == null)
|
||||||
|
{
|
||||||
|
throw new ArgumentNullException(nameof(builder));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (setupAction == null)
|
||||||
|
{
|
||||||
|
throw new ArgumentNullException(nameof(setupAction));
|
||||||
|
}
|
||||||
|
|
||||||
|
builder.Services.Configure(setupAction);
|
||||||
|
|
||||||
|
return builder;
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Sets the <see cref="CompatibilityVersion"/> for ASP.NET Core MVC for the application.
|
/// Sets the <see cref="CompatibilityVersion"/> for ASP.NET Core MVC for the application.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|
|
||||||
|
|
@ -9,6 +9,7 @@ using Microsoft.AspNetCore.Mvc.Controllers;
|
||||||
using Microsoft.AspNetCore.Mvc.Internal;
|
using Microsoft.AspNetCore.Mvc.Internal;
|
||||||
using Microsoft.AspNetCore.Mvc.MvcServiceCollectionExtensionsTestControllers;
|
using Microsoft.AspNetCore.Mvc.MvcServiceCollectionExtensionsTestControllers;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
using Microsoft.Extensions.Options;
|
||||||
using Moq;
|
using Moq;
|
||||||
using Xunit;
|
using Xunit;
|
||||||
|
|
||||||
|
|
@ -110,6 +111,33 @@ namespace Microsoft.AspNetCore.Mvc
|
||||||
Assert.Single(collection, d => d.ServiceType.Equals(typeof(ControllerTwo)));
|
Assert.Single(collection, d => d.ServiceType.Equals(typeof(ControllerTwo)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void ConfigureApiBehaviorOptions_InvokesSetupAction()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var serviceCollection = new ServiceCollection()
|
||||||
|
.AddOptions();
|
||||||
|
|
||||||
|
var builder = new MvcBuilder(
|
||||||
|
serviceCollection,
|
||||||
|
new ApplicationPartManager());
|
||||||
|
|
||||||
|
var part = new TestApplicationPart();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var result = builder.ConfigureApiBehaviorOptions(o =>
|
||||||
|
{
|
||||||
|
o.SuppressMapClientErrors = true;
|
||||||
|
});
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
var options = serviceCollection.
|
||||||
|
BuildServiceProvider()
|
||||||
|
.GetRequiredService<IOptions<ApiBehaviorOptions>>()
|
||||||
|
.Value;
|
||||||
|
Assert.True(options.SuppressMapClientErrors);
|
||||||
|
}
|
||||||
|
|
||||||
private class ControllerOne
|
private class ControllerOne
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,7 @@ using System.Reflection;
|
||||||
using Microsoft.AspNetCore.Mvc.ApplicationParts;
|
using Microsoft.AspNetCore.Mvc.ApplicationParts;
|
||||||
using Microsoft.AspNetCore.Mvc.Internal;
|
using Microsoft.AspNetCore.Mvc.Internal;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
using Microsoft.Extensions.Options;
|
||||||
using Moq;
|
using Moq;
|
||||||
using Xunit;
|
using Xunit;
|
||||||
|
|
||||||
|
|
@ -51,5 +52,32 @@ namespace Microsoft.AspNetCore.Mvc.DependencyInjection
|
||||||
Assert.Same(result, builder);
|
Assert.Same(result, builder);
|
||||||
Assert.Equal(new ApplicationPart[] { part }, builder.PartManager.ApplicationParts.ToArray());
|
Assert.Equal(new ApplicationPart[] { part }, builder.PartManager.ApplicationParts.ToArray());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void ConfigureApiBehaviorOptions_InvokesSetupAction()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var serviceCollection = new ServiceCollection()
|
||||||
|
.AddOptions();
|
||||||
|
|
||||||
|
var builder = new MvcCoreBuilder(
|
||||||
|
serviceCollection,
|
||||||
|
new ApplicationPartManager());
|
||||||
|
|
||||||
|
var part = new TestApplicationPart();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var result = builder.ConfigureApiBehaviorOptions(o =>
|
||||||
|
{
|
||||||
|
o.SuppressMapClientErrors = true;
|
||||||
|
});
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
var options = serviceCollection.
|
||||||
|
BuildServiceProvider()
|
||||||
|
.GetRequiredService<IOptions<ApiBehaviorOptions>>()
|
||||||
|
.Value;
|
||||||
|
Assert.True(options.SuppressMapClientErrors);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue