Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Routing] [Mvc] Support RoutePattern required values in matcher and link generator #4245

Merged
merged 2 commits into from
Dec 3, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ public void AttributeRouteEndpoints()
{
var endpointDataSource = CreateMvcEndpointDataSource(_attributeActionProvider);
var endpoints = endpointDataSource.Endpoints;

AssertHasEndpoints(endpoints);
}

[Benchmark]
Expand All @@ -66,6 +68,8 @@ public void ConventionalEndpoints()
var endpointDataSource = CreateMvcEndpointDataSource(_conventionalActionProvider);
endpointDataSource.ConventionalEndpointInfos.AddRange(_conventionalEndpointInfos);
var endpoints = endpointDataSource.Endpoints;

AssertHasEndpoints(endpoints);
}

private ActionDescriptor CreateAttributeRoutedAction(int id)
Expand Down Expand Up @@ -110,11 +114,20 @@ private MvcEndpointDataSource CreateMvcEndpointDataSource(
var dataSource = new MvcEndpointDataSource(
actionDescriptorCollectionProvider,
new MvcEndpointInvokerFactory(new ActionInvokerFactory(Array.Empty<IActionInvokerProvider>())),
new MockParameterPolicyFactory());
new MockParameterPolicyFactory(),
new MockRoutePatternTransformer());

return dataSource;
}

private class MockRoutePatternTransformer : RoutePatternTransformer
{
public override RoutePattern SubstituteRequiredValues(RoutePattern original, object requiredValues)
{
return original;
}
}

private class MockActionDescriptorCollectionProvider : IActionDescriptorCollectionProvider
{
public MockActionDescriptorCollectionProvider(List<ActionDescriptor> actionDescriptors)
Expand All @@ -137,5 +150,13 @@ public override IParameterPolicy Create(RoutePatternParameterPart parameter, IPa
throw new NotImplementedException();
}
}

private static void AssertHasEndpoints(IReadOnlyList<Http.Endpoint> endpoints)
{
if (endpoints.Count == 0)
{
throw new InvalidOperationException("Expected endpoints from data source.");
}
}
}
}
19 changes: 19 additions & 0 deletions src/Mvc/samples/MvcSandbox/SlugifyParameterTransformer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// 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.Text.RegularExpressions;
using Microsoft.AspNetCore.Routing;

namespace MvcSandbox
{
public class SlugifyParameterTransformer : IOutboundParameterTransformer
{
public string TransformOutbound(object value)
{
// Slugify value
return value == null ? null : Regex.Replace(value.ToString(), "([a-z])([A-Z])", "$1-$2", RegexOptions.None, TimeSpan.FromMilliseconds(100)).ToLower();
}
}
}

30 changes: 29 additions & 1 deletion src/Mvc/samples/MvcSandbox/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Routing;
using Microsoft.AspNetCore.Routing.Internal;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;

Expand All @@ -20,6 +22,10 @@ public class Startup
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddRouting(options =>
{
options.ConstraintMap["slugify"] = typeof(SlugifyParameterTransformer);
});
services.AddMvc().SetCompatibilityVersion(Microsoft.AspNetCore.Mvc.CompatibilityVersion.Latest);
}

Expand All @@ -37,6 +43,27 @@ public void Configure(IApplicationBuilder app)
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");

builder.MapControllerRoute(
name: "transform",
template: "Transform/{controller:slugify=Home}/{action:slugify=Index}/{id?}",
defaults: null,
constraints: new { controller = "Home" });

builder.MapGet(
"/graph",
"DFA Graph",
(httpContext) =>
{
using (var writer = new StreamWriter(httpContext.Response.Body, Encoding.UTF8, 1024, leaveOpen: true))
{
var graphWriter = httpContext.RequestServices.GetRequiredService<DfaGraphWriter>();
var dataSource = httpContext.RequestServices.GetRequiredService<EndpointDataSource>();
graphWriter.Write(dataSource, writer);
}

return Task.CompletedTask;
});

builder.MapApplication();

builder.MapHealthChecks("/healthz");
Expand All @@ -50,7 +77,7 @@ public void Configure(IApplicationBuilder app)

private static Task WriteEndpoints(HttpContext httpContext)
{
var dataSource = httpContext.RequestServices.GetRequiredService<CompositeEndpointDataSource>();
var dataSource = httpContext.RequestServices.GetRequiredService<EndpointDataSource>();

var sb = new StringBuilder();
sb.AppendLine("Endpoints:");
Expand Down Expand Up @@ -81,6 +108,7 @@ public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
factory
.AddConsole()
.AddDebug();
factory.SetMinimumLevel(LogLevel.Trace);
})
.UseIISIntegration()
.UseKestrel()
Expand Down
Loading