Skip to content
This repository was archived by the owner on Dec 14, 2018. It is now read-only.

Commit c9887e0

Browse files
committed
Simplify parameter transformer usage
1 parent a04dd48 commit c9887e0

File tree

10 files changed

+44
-31
lines changed

10 files changed

+44
-31
lines changed

src/Microsoft.AspNetCore.Mvc.Core/ApplicationModels/ActionModel.cs

+13
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ public ActionModel(ActionModel other)
5050

5151
ActionMethod = other.ActionMethod;
5252
ActionName = other.ActionName;
53+
RouteParameterTransformer = other.RouteParameterTransformer;
5354

5455
// Not making a deep copy of the controller, this action still belongs to the same controller.
5556
Controller = other.Controller;
@@ -93,6 +94,18 @@ public ActionModel(ActionModel other)
9394

9495
public IList<ParameterModel> Parameters { get; }
9596

97+
/// <summary>
98+
/// Gets or sets an <see cref="IOutboundParameterTransformer"/> that will be used to transform
99+
/// built-in route parameters such as <c>action</c>, <c>controller</c>, and <c>area</c> as well as
100+
/// additional parameters specified by <see cref="RouteValues"/> into static segments in the route template.
101+
/// </summary>
102+
/// <remarks>
103+
/// <para>
104+
/// This feature only applies when using endpoint routing.
105+
/// </para>
106+
/// </remarks>
107+
public IOutboundParameterTransformer RouteParameterTransformer { get; set; }
108+
96109
/// <summary>
97110
/// Gets a collection of route values that must be present in the
98111
/// <see cref="RouteData.Values"/> for the corresponding action to be selected.

src/Microsoft.AspNetCore.Mvc.Core/ApplicationModels/ApplicationModelFactory.cs

+2-5
Original file line numberDiff line numberDiff line change
@@ -159,20 +159,17 @@ private static void ReplaceAttributeRouteTokens(
159159
routeValues.TryAdd(kvp.Key, kvp.Value);
160160
}
161161

162-
action.Properties.TryGetValue(typeof(IOutboundParameterTransformer), out var obj);
163-
var transformer = obj as IOutboundParameterTransformer;
164-
165162
selector.AttributeRouteModel.Template = AttributeRouteModel.ReplaceTokens(
166163
selector.AttributeRouteModel.Template,
167164
routeValues,
168-
transformer);
165+
action.RouteParameterTransformer);
169166

170167
if (selector.AttributeRouteModel.Name != null)
171168
{
172169
selector.AttributeRouteModel.Name = AttributeRouteModel.ReplaceTokens(
173170
selector.AttributeRouteModel.Name,
174171
routeValues,
175-
transformer);
172+
action.RouteParameterTransformer);
176173
}
177174
}
178175
catch (InvalidOperationException ex)

src/Microsoft.AspNetCore.Mvc.Core/ApplicationModels/RouteTokenTransformerConvention.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public void Apply(ActionModel action)
3333
{
3434
if (ShouldApply(action))
3535
{
36-
action.Properties[typeof(IOutboundParameterTransformer)] = _parameterTransformer;
36+
action.RouteParameterTransformer = _parameterTransformer;
3737
}
3838
}
3939

src/Microsoft.AspNetCore.Mvc.RazorPages/ApplicationModels/PageRouteModel.cs

+13
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ public PageRouteModel(PageRouteModel other)
5555
RelativePath = other.RelativePath;
5656
ViewEnginePath = other.ViewEnginePath;
5757
AreaName = other.AreaName;
58+
RouteParameterTransformer = other.RouteParameterTransformer;
5859

5960
Properties = new Dictionary<object, object>(other.Properties);
6061
Selectors = new List<SelectorModel>(other.Selectors.Select(m => new SelectorModel(m)));
@@ -113,5 +114,17 @@ public PageRouteModel(PageRouteModel other)
113114
/// </para>
114115
/// </remarks>
115116
public IDictionary<string, string> RouteValues { get; }
117+
118+
/// <summary>
119+
/// Gets or sets an <see cref="IOutboundParameterTransformer"/> that will be used to transform
120+
/// built-in route parameters such as <c>action</c>, <c>controller</c>, and <c>area</c> as well as
121+
/// additional parameters specified by <see cref="RouteValues"/> into static segments in the route template.
122+
/// </summary>
123+
/// <remarks>
124+
/// <para>
125+
/// This feature only applies when using endpoint routing.
126+
/// </para>
127+
/// </remarks>
128+
public IOutboundParameterTransformer RouteParameterTransformer { get; set; }
116129
}
117130
}

src/Microsoft.AspNetCore.Mvc.RazorPages/ApplicationModels/PageRouteTransformerConvention.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public void Apply(PageRouteModel model)
3333
{
3434
if (ShouldApply(model))
3535
{
36-
model.Properties[typeof(IOutboundParameterTransformer)] = _parameterTransformer;
36+
model.RouteParameterTransformer = _parameterTransformer;
3737
}
3838
}
3939

src/Microsoft.AspNetCore.Mvc.RazorPages/Infrastructure/PageActionDescriptorProvider.cs

+2-5
Original file line numberDiff line numberDiff line change
@@ -113,11 +113,8 @@ private void AddActionDescriptors(IList<ActionDescriptor> actions, PageRouteMode
113113

114114
private static string TransformPageRoute(PageRouteModel model, SelectorModel selectorModel)
115115
{
116-
model.Properties.TryGetValue(typeof(IOutboundParameterTransformer), out var transformer);
117-
var pageRouteTransformer = transformer as IOutboundParameterTransformer;
118-
119116
// Transformer not set on page route
120-
if (pageRouteTransformer == null)
117+
if (model.RouteParameterTransformer == null)
121118
{
122119
return selectorModel.AttributeRouteModel.Template;
123120
}
@@ -134,7 +131,7 @@ private static string TransformPageRoute(PageRouteModel model, SelectorModel sel
134131
var segments = pageRouteMetadata.PageRoute.Split('/');
135132
for (var i = 0; i < segments.Length; i++)
136133
{
137-
segments[i] = pageRouteTransformer.TransformOutbound(segments[i]);
134+
segments[i] = model.RouteParameterTransformer.TransformOutbound(segments[i]);
138135
}
139136

140137
var transformedPageRoute = string.Join("/", segments);

test/Microsoft.AspNetCore.Mvc.Core.Test/ApplicationModels/ActionModelTest.cs

+3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
using Microsoft.AspNetCore.Mvc.ActionConstraints;
88
using Microsoft.AspNetCore.Mvc.Filters;
99
using Microsoft.AspNetCore.Mvc.Routing;
10+
using Microsoft.AspNetCore.Routing;
11+
using Moq;
1012
using Xunit;
1113

1214
namespace Microsoft.AspNetCore.Mvc.ApplicationModels
@@ -75,6 +77,7 @@ public void CopyConstructor_CopiesAllProperties()
7577
(typeof(TestController).GetTypeInfo(),
7678
new List<object>());
7779
action.Filters.Add(new MyFilterAttribute());
80+
action.RouteParameterTransformer = Mock.Of<IOutboundParameterTransformer>();
7881
action.RouteValues.Add("key", "value");
7982
action.Properties.Add(new KeyValuePair<object, object>("test key", "test value"));
8083

test/Microsoft.AspNetCore.Mvc.Core.Test/ApplicationModels/RouteTokenTransformerConventionTest.cs

+3-5
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,10 @@
33

44
using System;
55
using System.Reflection;
6-
using Microsoft.AspNetCore.Mvc.ApplicationModels;
76
using Microsoft.AspNetCore.Routing;
87
using Xunit;
98

10-
namespace Microsoft.AspNetCore.Mvc.Test.ApplicationModels
9+
namespace Microsoft.AspNetCore.Mvc.ApplicationModels
1110
{
1211
public class RouteTokenTransformerConventionTest
1312
{
@@ -28,8 +27,7 @@ public void Apply_HasAttributeRouteModel_SetRouteTokenTransformer()
2827
convention.Apply(model);
2928

3029
// Assert
31-
Assert.True(model.Properties.TryGetValue(typeof(IOutboundParameterTransformer), out var routeTokenTransformer));
32-
Assert.Equal(transformer, routeTokenTransformer);
30+
Assert.Same(transformer, model.RouteParameterTransformer);
3331
}
3432

3533
[Fact]
@@ -49,7 +47,7 @@ public void Apply_ShouldApplyFalse_NoOp()
4947
convention.Apply(model);
5048

5149
// Assert
52-
Assert.False(model.Properties.TryGetValue(typeof(IOutboundParameterTransformer), out _));
50+
Assert.Null(model.RouteParameterTransformer);
5351
}
5452

5553
private MethodInfo GetMethodInfo()

test/Microsoft.AspNetCore.Mvc.RazorPages.Test/ApplicationModels/PageRouteTransformerConventionTest.cs

+2-3
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@ public void Apply_SetTransformer()
2424
convention.Apply(model);
2525

2626
// Assert
27-
Assert.True(model.Properties.TryGetValue(typeof(IOutboundParameterTransformer), out var routeTokenTransformer));
28-
Assert.Equal(transformer, routeTokenTransformer);
27+
Assert.Same(transformer, model.RouteParameterTransformer);
2928
}
3029

3130
[Fact]
@@ -41,7 +40,7 @@ public void Apply_ShouldApplyFalse_NoOp()
4140
convention.Apply(model);
4241

4342
// Assert
44-
Assert.False(model.Properties.TryGetValue(typeof(IOutboundParameterTransformer), out _));
43+
Assert.Null(model.RouteParameterTransformer);
4544
}
4645

4746
private class TestParameterTransformer : IOutboundParameterTransformer

test/WebSites/RoutingWebSite/ControllerRouteTokenTransformerConvention.cs

+4-11
Original file line numberDiff line numberDiff line change
@@ -8,31 +8,24 @@
88

99
namespace RoutingWebSite
1010
{
11-
public class ControllerRouteTokenTransformerConvention : IApplicationModelConvention
11+
public class ControllerRouteTokenTransformerConvention : RouteTokenTransformerConvention
1212
{
1313
private readonly Type _controllerType;
14-
private readonly IOutboundParameterTransformer _parameterTransformer;
1514

1615
public ControllerRouteTokenTransformerConvention(Type controllerType, IOutboundParameterTransformer parameterTransformer)
16+
: base(parameterTransformer)
1717
{
1818
if (parameterTransformer == null)
1919
{
2020
throw new ArgumentNullException(nameof(parameterTransformer));
2121
}
2222

2323
_controllerType = controllerType;
24-
_parameterTransformer = parameterTransformer;
2524
}
2625

27-
public void Apply(ApplicationModel application)
26+
protected override bool ShouldApply(ActionModel action)
2827
{
29-
foreach (var controller in application.Controllers.Where(c => c.ControllerType == _controllerType))
30-
{
31-
foreach (var action in controller.Actions)
32-
{
33-
action.Properties[typeof(IOutboundParameterTransformer)] = _parameterTransformer;
34-
}
35-
}
28+
return action.Controller.ControllerType == _controllerType;
3629
}
3730
}
3831
}

0 commit comments

Comments
 (0)