Resolved #3880: Obsolete app.UseMvcWithDefaultRouteAndArea() and introduce app.UseConfiguredEndpoints().

pull/3947/head
Halil İbrahim Kalkan 5 years ago
parent ba6689f238
commit 6c934e53e6

@ -15,18 +15,12 @@ namespace Microsoft.AspNetCore.Builder
/// <param name="app">The <see cref="T:Microsoft.AspNetCore.Builder.IApplicationBuilder" />.</param>
/// <param name="additionalConfigurationAction">Additional action to configure routes</param>
/// <returns>A reference to this instance after the operation has completed.</returns>
[Obsolete("Use app.UseConfiguredEndpoints(...) extension method instead!")]
public static IApplicationBuilder UseMvcWithDefaultRouteAndArea(
this IApplicationBuilder app,
Action<IEndpointRouteBuilder> additionalConfigurationAction = null)
{
return app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute("defaultWithArea", "{area}/{controller=Home}/{action=Index}/{id?}");
endpoints.MapControllerRoute("default", "{controller=Home}/{action=Index}/{id?}");
endpoints.MapRazorPages();
additionalConfigurationAction?.Invoke(endpoints);
});
return app.UseConfiguredEndpoints(additionalConfigurationAction);
}
}
}

@ -13,10 +13,12 @@ using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Reflection;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Mvc.ApiExplorer;
using Microsoft.AspNetCore.Mvc.Razor;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure;
using Microsoft.AspNetCore.Routing;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Localization;
using Volo.Abp.ApiVersioning;
@ -165,6 +167,16 @@ namespace Volo.Abp.AspNetCore.Mvc
{
mvcOptions.AddAbp(context.Services);
});
Configure<AbpEndpointRouterOptions>(options =>
{
options.EndpointConfigureActions.Add(context =>
{
context.Endpoints.MapControllerRoute("defaultWithArea", "{area}/{controller=Home}/{action=Index}/{id?}");
context.Endpoints.MapControllerRoute("default", "{controller=Home}/{action=Index}/{id?}");
context.Endpoints.MapRazorPages();
});
});
}
public override void OnApplicationInitialization(ApplicationInitializationContext context)

@ -29,8 +29,13 @@ namespace Volo.Abp.AspNetCore.Mvc.DependencyInjection
foreach (var serviceType in serviceTypes)
{
var serviceDescriptor = ServiceDescriptor.Describe(serviceType, type, lifeTime);
services.Add(serviceDescriptor);
services.Add(
ServiceDescriptor.Describe(
serviceType,
type,
lifeTime
)
);
}
}

@ -0,0 +1,47 @@
using System;
using System.Linq;
using Microsoft.AspNetCore.Routing;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
namespace Microsoft.AspNetCore.Builder
{
public static class AbpAspNetCoreApplicationBuilderExtensions
{
/// <summary>
/// Maps endpoints configured with the <see cref="AbpEndpointRouterOptions"/>.
/// It internally uses the standard app.UseEndpoints(...) method.
/// </summary>
/// <param name="app">The application builder</param>
/// <param name="additionalConfigurationAction">Additional (and optional) endpoint configuration</param>
/// <returns></returns>
public static IApplicationBuilder UseConfiguredEndpoints(
this IApplicationBuilder app,
Action<IEndpointRouteBuilder> additionalConfigurationAction = null)
{
var options = app.ApplicationServices
.GetRequiredService<IOptions<AbpEndpointRouterOptions>>()
.Value;
if (!options.EndpointConfigureActions.Any())
{
return app;
}
return app.UseEndpoints(endpoints =>
{
using (var scope = app.ApplicationServices.CreateScope())
{
var context = new EndpointRouteBuilderContext(endpoints, scope.ServiceProvider);
foreach (var configureAction in options.EndpointConfigureActions)
{
configureAction(context);
}
additionalConfigurationAction?.Invoke(endpoints);
}
});
}
}
}

@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
namespace Microsoft.AspNetCore.Routing
{
public class AbpEndpointRouterOptions
{
public List<Action<EndpointRouteBuilderContext>> EndpointConfigureActions { get; }
public AbpEndpointRouterOptions()
{
EndpointConfigureActions = new List<Action<EndpointRouteBuilderContext>>();
}
}
}

@ -0,0 +1,18 @@
using System;
namespace Microsoft.AspNetCore.Routing
{
public class EndpointRouteBuilderContext
{
public IEndpointRouteBuilder Endpoints { get; }
public IServiceProvider ScopeServiceProvider { get; }
public EndpointRouteBuilderContext(
IEndpointRouteBuilder endpoints,
IServiceProvider scopeServiceProvider)
{
Endpoints = endpoints;
ScopeServiceProvider = scopeServiceProvider;
}
}
}
Loading…
Cancel
Save