Change MvcEndpointInfo to internal (#8210)

This commit is contained in:
James Newton-King 2018-08-03 16:30:57 +12:00 committed by GitHub
parent 90b093baac
commit ac410b76d9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 1 additions and 584 deletions

View File

@ -171,38 +171,6 @@ namespace Microsoft.AspNetCore.Builder
}
}
public static IApplicationBuilder UseMvcWithEndpoint(
this IApplicationBuilder app,
Action<MvcEndpointInfoBuilder> configureRoutes)
{
if (app == null)
{
throw new ArgumentNullException(nameof(app));
}
if (configureRoutes == null)
{
throw new ArgumentNullException(nameof(configureRoutes));
}
VerifyMvcIsRegistered(app);
var mvcEndpointDataSource = app.ApplicationServices
.GetRequiredService<IEnumerable<EndpointDataSource>>()
.OfType<MvcEndpointDataSource>()
.First();
var constraintResolver = app.ApplicationServices.GetRequiredService<IInlineConstraintResolver>();
MvcEndpointInfoBuilder routeBuilder = new MvcEndpointInfoBuilder(constraintResolver);
configureRoutes(routeBuilder);
mvcEndpointDataSource.ConventionalEndpointInfos.AddRange(routeBuilder.EndpointInfos);
return app.UseEndpoint();
}
private static void VerifyMvcIsRegistered(IApplicationBuilder app)
{
// Verify if AddMvc was done before calling UseMvc

View File

@ -9,7 +9,7 @@ using Microsoft.AspNetCore.Routing.Template;
namespace Microsoft.AspNetCore.Builder
{
public class MvcEndpointInfo
internal class MvcEndpointInfo
{
public MvcEndpointInfo(
string name,

View File

@ -1,19 +0,0 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Collections.Generic;
using Microsoft.AspNetCore.Routing;
namespace Microsoft.AspNetCore.Builder
{
public class MvcEndpointInfoBuilder
{
public MvcEndpointInfoBuilder(IInlineConstraintResolver constraintResolver)
{
ConstraintResolver = constraintResolver;
}
public List<MvcEndpointInfo> EndpointInfos { get; } = new List<MvcEndpointInfo>();
public IInlineConstraintResolver ConstraintResolver { get; }
}
}

View File

@ -1,229 +0,0 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using Microsoft.AspNetCore.Mvc.Core;
using Microsoft.AspNetCore.Routing;
using Microsoft.AspNetCore.Routing.Constraints;
namespace Microsoft.AspNetCore.Builder
{
/// <summary>
/// Provides extension methods for <see cref="MvcEndpointInfoBuilder" /> to add endpoints.
/// </summary>
public static class MvcEndpointInfoBuilderExtensions
{
#region MapEndpoint
/// <summary>
/// Adds a endpoint to the <see cref="MvcEndpointInfoBuilder" /> with the specified name and template.
/// </summary>
/// <param name="endpointBuilder">The <see cref="MvcEndpointInfoBuilder" /> to add the endpoint to.</param>
/// <param name="name">The name of the endpoint.</param>
/// <param name="template">The URL pattern of the endpoint.</param>
/// <returns>A reference to this instance after the operation has completed.</returns>
public static MvcEndpointInfoBuilder MapEndpoint(this MvcEndpointInfoBuilder endpointBuilder, string name, string template)
{
endpointBuilder.MapEndpoint(name, template, null);
return endpointBuilder;
}
/// <summary>
/// Adds a endpoint to the <see cref="MvcEndpointInfoBuilder" /> with the specified name, template, and default values.
/// </summary>
/// <param name="endpointBuilder">The <see cref="MvcEndpointInfoBuilder" /> to add the endpoint to.</param>
/// <param name="name">The name of the endpoint.</param>
/// <param name="template">The URL pattern of the endpoint.</param>
/// <param name="defaults">
/// An object that contains default values for endpoint parameters. The object's properties represent the names
/// and values of the default values.
/// </param>
/// <returns>A reference to this instance after the operation has completed.</returns>
public static MvcEndpointInfoBuilder MapEndpoint(this MvcEndpointInfoBuilder endpointBuilder, string name, string template, object defaults)
{
return endpointBuilder.MapEndpoint(name, template, defaults, null);
}
/// <summary>
/// Adds a endpoint to the <see cref="MvcEndpointInfoBuilder" /> with the specified name, template, default values, and
/// constraints.
/// </summary>
/// <param name="endpointBuilder">The <see cref="MvcEndpointInfoBuilder" /> to add the endpoint to.</param>
/// <param name="name">The name of the endpoint.</param>
/// <param name="template">The URL pattern of the endpoint.</param>
/// <param name="defaults">
/// An object that contains default values for endpoint parameters. The object's properties represent the names
/// and values of the default values.
/// </param>
/// <param name="constraints">
/// An object that contains constraints for the endpoint. The object's properties represent the names and values
/// of the constraints.
/// </param>
/// <returns>A reference to this instance after the operation has completed.</returns>
public static MvcEndpointInfoBuilder MapEndpoint(this MvcEndpointInfoBuilder endpointBuilder, string name, string template, object defaults, object constraints)
{
return endpointBuilder.MapEndpoint(name, template, defaults, constraints, null);
}
/// <summary>
/// Adds a endpoint to the <see cref="MvcEndpointInfoBuilder" /> with the specified name, template, default values, and
/// data tokens.
/// </summary>
/// <param name="endpointBuilder">The <see cref="MvcEndpointInfoBuilder" /> to add the endpoint to.</param>
/// <param name="name">The name of the endpoint.</param>
/// <param name="template">The URL pattern of the endpoint.</param>
/// <param name="defaults">
/// An object that contains default values for endpoint parameters. The object's properties represent the names
/// and values of the default values.
/// </param>
/// <param name="constraints">
/// An object that contains constraints for the endpoint. The object's properties represent the names and values
/// of the constraints.
/// </param>
/// <param name="dataTokens">
/// An object that contains data tokens for the endpoint. The object's properties represent the names and values
/// of the data tokens.
/// </param>
/// <returns>A reference to this instance after the operation has completed.</returns>
public static MvcEndpointInfoBuilder MapEndpoint(this MvcEndpointInfoBuilder endpointBuilder, string name, string template, object defaults, object constraints, object dataTokens)
{
endpointBuilder.EndpointInfos.Add(new MvcEndpointInfo(
name,
template,
new RouteValueDictionary(defaults),
new RouteValueDictionary(constraints),
new RouteValueDictionary(dataTokens),
endpointBuilder.ConstraintResolver));
return endpointBuilder;
}
#endregion
#region MapAreaEndpoint
/// <summary>
/// Adds a endpoint to the <see cref="MvcEndpointInfoBuilder"/> with the given MVC area with the specified
/// <paramref name="name"/>, <paramref name="areaName"/> and <paramref name="template"/>.
/// </summary>
/// <param name="endpointBuilder">The <see cref="MvcEndpointInfoBuilder"/> to add the endpoint to.</param>
/// <param name="name">The name of the endpoint.</param>
/// <param name="areaName">The MVC area name.</param>
/// <param name="template">The URL pattern of the endpoint.</param>
/// <returns>A reference to this instance after the operation has completed.</returns>
public static MvcEndpointInfoBuilder MapAreaEndpoint(
this MvcEndpointInfoBuilder endpointBuilder,
string name,
string areaName,
string template)
{
MapAreaEndpoint(endpointBuilder, name, areaName, template, defaults: null, constraints: null, dataTokens: null);
return endpointBuilder;
}
/// <summary>
/// Adds a endpoint to the <see cref="MvcEndpointInfoBuilder"/> with the given MVC area with the specified
/// <paramref name="name"/>, <paramref name="areaName"/>, <paramref name="template"/>, and
/// <paramref name="defaults"/>.
/// </summary>
/// <param name="endpointBuilder">The <see cref="MvcEndpointInfoBuilder"/> to add the endpoint to.</param>
/// <param name="name">The name of the endpoint.</param>
/// <param name="areaName">The MVC area name.</param>
/// <param name="template">The URL pattern of the endpoint.</param>
/// <param name="defaults">
/// An object that contains default values for endpoint parameters. The object's properties represent the
/// names and values of the default values.
/// </param>
/// <returns>A reference to this instance after the operation has completed.</returns>
public static MvcEndpointInfoBuilder MapAreaEndpoint(
this MvcEndpointInfoBuilder endpointBuilder,
string name,
string areaName,
string template,
object defaults)
{
MapAreaEndpoint(endpointBuilder, name, areaName, template, defaults, constraints: null, dataTokens: null);
return endpointBuilder;
}
/// <summary>
/// Adds a endpoint to the <see cref="MvcEndpointInfoBuilder"/> with the given MVC area with the specified
/// <paramref name="name"/>, <paramref name="areaName"/>, <paramref name="template"/>,
/// <paramref name="defaults"/>, and <paramref name="constraints"/>.
/// </summary>
/// <param name="endpointBuilder">The <see cref="MvcEndpointInfoBuilder"/> to add the endpoint to.</param>
/// <param name="name">The name of the endpoint.</param>
/// <param name="areaName">The MVC area name.</param>
/// <param name="template">The URL pattern of the endpoint.</param>
/// <param name="defaults">
/// An object that contains default values for endpoint parameters. The object's properties represent the
/// names and values of the default values.
/// </param>
/// <param name="constraints">
/// An object that contains constraints for the endpoint. The object's properties represent the names and
/// values of the constraints.
/// </param>
/// <returns>A reference to this instance after the operation has completed.</returns>
public static MvcEndpointInfoBuilder MapAreaEndpoint(
this MvcEndpointInfoBuilder endpointBuilder,
string name,
string areaName,
string template,
object defaults,
object constraints)
{
MapAreaEndpoint(endpointBuilder, name, areaName, template, defaults, constraints, dataTokens: null);
return endpointBuilder;
}
/// <summary>
/// Adds a endpoint to the <see cref="MvcEndpointInfoBuilder"/> with the given MVC area with the specified
/// <paramref name="name"/>, <paramref name="areaName"/>, <paramref name="template"/>,
/// <paramref name="defaults"/>, <paramref name="constraints"/>, and <paramref name="dataTokens"/>.
/// </summary>
/// <param name="endpointBuilder">The <see cref="MvcEndpointInfoBuilder"/> to add the endpoint to.</param>
/// <param name="name">The name of the endpoint.</param>
/// <param name="areaName">The MVC area name.</param>
/// <param name="template">The URL pattern of the endpoint.</param>
/// <param name="defaults">
/// An object that contains default values for endpoint parameters. The object's properties represent the
/// names and values of the default values.
/// </param>
/// <param name="constraints">
/// An object that contains constraints for the endpoint. The object's properties represent the names and
/// values of the constraints.
/// </param>
/// <param name="dataTokens">
/// An object that contains data tokens for the endpoint. The object's properties represent the names and
/// values of the data tokens.
/// </param>
/// <returns>A reference to this instance after the operation has completed.</returns>
public static MvcEndpointInfoBuilder MapAreaEndpoint(
this MvcEndpointInfoBuilder endpointBuilder,
string name,
string areaName,
string template,
object defaults,
object constraints,
object dataTokens)
{
if (endpointBuilder == null)
{
throw new ArgumentNullException(nameof(endpointBuilder));
}
if (string.IsNullOrEmpty(areaName))
{
throw new ArgumentException(Resources.ArgumentCannotBeNullOrEmpty, nameof(areaName));
}
var defaultsDictionary = new RouteValueDictionary(defaults);
defaultsDictionary["area"] = defaultsDictionary["area"] ?? areaName;
var constraintsDictionary = new RouteValueDictionary(constraints);
constraintsDictionary["area"] = constraintsDictionary["area"] ?? new StringRouteConstraint(areaName);
endpointBuilder.MapEndpoint(name, template, defaultsDictionary, constraintsDictionary, dataTokens);
return endpointBuilder;
}
#endregion
}
}

View File

@ -442,7 +442,6 @@ namespace Microsoft.AspNetCore.Mvc.Internal
}
}
// REVIEW: Infos added after endpoints are initialized will not be used
public List<MvcEndpointInfo> ConventionalEndpointInfos { get; }
private class RouteNameMetadata : IRouteNameMetadata

View File

@ -37,26 +37,6 @@ namespace Microsoft.AspNetCore.Mvc.Core.Builder
exception.Message);
}
[Fact]
public void UseMvcWithEndpoint_ThrowsInvalidOperationException_IfMvcMarkerServiceIsNotRegistered()
{
// Arrange
var applicationBuilderMock = new Mock<IApplicationBuilder>();
applicationBuilderMock
.Setup(s => s.ApplicationServices)
.Returns(Mock.Of<IServiceProvider>());
// Act & Assert
var exception = Assert.Throws<InvalidOperationException>(
() => applicationBuilderMock.Object.UseMvcWithEndpoint(rb => { }));
Assert.Equal(
"Unable to find the required services. Please add all the required services by calling " +
"'IServiceCollection.AddMvc' inside the call to 'ConfigureServices(...)' " +
"in the application startup code.",
exception.Message);
}
[Fact]
public void UseMvc_EndpointRoutingDisabled_NoEndpointInfos()
{

View File

@ -1,282 +0,0 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Routing;
using Microsoft.AspNetCore.Routing.Constraints;
using Moq;
using Xunit;
namespace Microsoft.AspNetCore.Mvc.Core.Test.Builder
{
public class MvcEndpointInfoBuilderExtensionsTest
{
#region MapAreaEndpoint
[Fact]
public void MapAreaEndpoint_Simple()
{
// Arrange
var builder = CreateEndpointBuilder();
// Act
builder.MapAreaEndpoint(name: null, areaName: "admin", template: "site/Admin/");
// Assert
var endpointInfo = Assert.Single(builder.EndpointInfos);
Assert.Null(endpointInfo.Name);
Assert.Equal("site/Admin/", endpointInfo.Template);
Assert.Collection(
endpointInfo.Constraints.OrderBy(kvp => kvp.Key),
kvp =>
{
Assert.Equal("area", kvp.Key);
Assert.IsType<StringRouteConstraint>(kvp.Value);
});
Assert.Empty(endpointInfo.DataTokens);
Assert.Collection(
endpointInfo.Defaults.OrderBy(kvp => kvp.Key),
kvp =>
{
Assert.Equal("area", kvp.Key);
Assert.Equal("admin", kvp.Value);
});
}
[Fact]
public void MapAreaEndpoint_Defaults()
{
// Arrange
var builder = CreateEndpointBuilder();
// Act
builder.MapAreaEndpoint(
name: "admin_area",
areaName: "admin",
template: "site/Admin/",
defaults: new { action = "Home" });
// Assert
var endpointInfo = Assert.Single(builder.EndpointInfos);
Assert.Equal("admin_area", endpointInfo.Name);
Assert.Equal("site/Admin/", endpointInfo.Template);
Assert.Collection(
endpointInfo.Constraints.OrderBy(kvp => kvp.Key),
kvp =>
{
Assert.Equal("area", kvp.Key);
Assert.IsType<StringRouteConstraint>(kvp.Value);
});
Assert.Empty(endpointInfo.DataTokens);
Assert.Collection(
endpointInfo.Defaults.OrderBy(kvp => kvp.Key),
kvp =>
{
Assert.Equal("action", kvp.Key);
Assert.Equal("Home", kvp.Value);
},
kvp =>
{
Assert.Equal("area", kvp.Key);
Assert.Equal("admin", kvp.Value);
});
}
[Fact]
public void MapAreaEndpoint_DefaultsAndConstraints()
{
// Arrange
var builder = CreateEndpointBuilder();
// Act
builder.MapAreaEndpoint(
name: "admin_area",
areaName: "admin",
template: "site/Admin/",
defaults: new { action = "Home" },
constraints: new { id = new IntRouteConstraint() });
// Assert
var endpointInfo = Assert.Single(builder.EndpointInfos);
Assert.Equal("admin_area", endpointInfo.Name);
Assert.Equal("site/Admin/", endpointInfo.Template);
Assert.Collection(
endpointInfo.Constraints.OrderBy(kvp => kvp.Key),
kvp =>
{
Assert.Equal("area", kvp.Key);
Assert.IsType<StringRouteConstraint>(kvp.Value);
},
kvp =>
{
Assert.Equal("id", kvp.Key);
Assert.IsType<IntRouteConstraint>(kvp.Value);
});
Assert.Empty(endpointInfo.DataTokens);
Assert.Collection(
endpointInfo.Defaults.OrderBy(kvp => kvp.Key),
kvp =>
{
Assert.Equal("action", kvp.Key);
Assert.Equal("Home", kvp.Value);
},
kvp =>
{
Assert.Equal("area", kvp.Key);
Assert.Equal("admin", kvp.Value);
});
}
[Fact]
public void MapAreaEndpoint_DefaultsConstraintsAndDataTokens()
{
// Arrange
var builder = CreateEndpointBuilder();
// Act
builder.MapAreaEndpoint(
name: "admin_area",
areaName: "admin",
template: "site/Admin/",
defaults: new { action = "Home" },
constraints: new { id = new IntRouteConstraint() },
dataTokens: new { some_token = "hello" });
// Assert
var endpointInfo = Assert.Single(builder.EndpointInfos);
Assert.Equal("admin_area", endpointInfo.Name);
Assert.Equal("site/Admin/", endpointInfo.Template);
Assert.Collection(
endpointInfo.Constraints.OrderBy(kvp => kvp.Key),
kvp =>
{
Assert.Equal("area", kvp.Key);
Assert.IsType<StringRouteConstraint>(kvp.Value);
},
kvp =>
{
Assert.Equal("id", kvp.Key);
Assert.IsType<IntRouteConstraint>(kvp.Value);
});
Assert.Collection(
endpointInfo.DataTokens.OrderBy(kvp => kvp.Key),
kvp =>
{
Assert.Equal("some_token", kvp.Key);
Assert.Equal("hello", kvp.Value);
});
Assert.Collection(
endpointInfo.Defaults.OrderBy(kvp => kvp.Key),
kvp =>
{
Assert.Equal("action", kvp.Key);
Assert.Equal("Home", kvp.Value);
},
kvp =>
{
Assert.Equal("area", kvp.Key);
Assert.Equal("admin", kvp.Value);
});
}
[Fact]
public void MapAreaEndpoint_DoesNotReplaceValuesForAreaIfAlreadyPresentInConstraintsOrDefaults()
{
// Arrange
var builder = CreateEndpointBuilder();
// Act
builder.MapAreaEndpoint(
name: "admin_area",
areaName: "admin",
template: "site/Admin/",
defaults: new { area = "Home" },
constraints: new { area = new IntRouteConstraint() },
dataTokens: new { some_token = "hello" });
// Assert
var endpointInfo = Assert.Single(builder.EndpointInfos);
Assert.Equal("admin_area", endpointInfo.Name);
Assert.Equal("site/Admin/", endpointInfo.Template);
Assert.Collection(
endpointInfo.Constraints.OrderBy(kvp => kvp.Key),
kvp =>
{
Assert.Equal("area", kvp.Key);
Assert.IsType<IntRouteConstraint>(kvp.Value);
});
Assert.Collection(
endpointInfo.DataTokens.OrderBy(kvp => kvp.Key),
kvp =>
{
Assert.Equal("some_token", kvp.Key);
Assert.Equal("hello", kvp.Value);
});
Assert.Collection(
endpointInfo.Defaults.OrderBy(kvp => kvp.Key),
kvp =>
{
Assert.Equal("area", kvp.Key);
Assert.Equal("Home", kvp.Value);
});
}
[Fact]
public void MapAreaEndpoint_UsesPassedInAreaNameAsIs()
{
// Arrange
var builder = CreateEndpointBuilder();
var areaName = "user.admin";
// Act
builder.MapAreaEndpoint(name: null, areaName: areaName, template: "site/Admin/");
// Assert
var endpointInfo = Assert.Single(builder.EndpointInfos);
Assert.Null(endpointInfo.Name);
Assert.Equal("site/Admin/", endpointInfo.Template);
Assert.Collection(
endpointInfo.Constraints.OrderBy(kvp => kvp.Key),
kvp =>
{
Assert.Equal("area", kvp.Key);
Assert.IsType<StringRouteConstraint>(kvp.Value);
var values = new RouteValueDictionary(new { area = areaName });
var match = kvp.Value.Match(
new DefaultHttpContext(),
route: new Mock<IRouter>().Object,
routeKey: kvp.Key,
values: values,
routeDirection: RouteDirection.UrlGeneration);
Assert.True(match);
});
Assert.Empty(endpointInfo.DataTokens);
Assert.Collection(
endpointInfo.Defaults.OrderBy(kvp => kvp.Key),
kvp =>
{
Assert.Equal("area", kvp.Key);
Assert.Equal(kvp.Value, areaName);
});
}
#endregion
private MvcEndpointInfoBuilder CreateEndpointBuilder()
{
var builder = new MvcEndpointInfoBuilder(Mock.Of<IInlineConstraintResolver>());
return builder;
}
}
}