@@ -57,24 +57,25 @@ public class AnchorTagHelper : TagHelper
57
57
public string Route { get ; set ; }
58
58
59
59
/// <inheritdoc />
60
- /// <remarks>Does nothing if user provides an "href" attribute. Cannot specify an "href" attribute AND
61
- /// <see cref="Action"/>, <see cref="Controller"/> or <see cref="Route"/>.</remarks>
60
+ /// <remarks>Does nothing if user provides an "href" attribute. Providing an "href" attribute and
61
+ /// <see cref="Action"/>, <see cref="Controller"/> or <see cref="Route"/> throws an
62
+ /// <see cref="InvalidOperationException"/>.</remarks>
62
63
public override void Process ( TagHelperContext context , TagHelperOutput output )
63
64
{
64
- var routeValues = GetRouteValues ( output ) ;
65
+ var routePrefixedAttributes = output . FindPrefixedAttributes ( RouteAttributePrefix ) ;
65
66
66
67
// If there's an "href" on the tag it means it's being used as a normal anchor.
67
68
if ( output . Attributes . ContainsKey ( Href ) )
68
69
{
69
- if ( Action != null || Controller != null || Route != null || routeValues != null )
70
+ if ( Action != null || Controller != null || Route != null || routePrefixedAttributes . Any ( ) )
70
71
{
71
72
// User specified an href AND a Action, Controller or Route; can't determine the href attribute.
72
73
throw new InvalidOperationException (
73
- Resources . FormatAnchorTagHelper_CannotDetermineHrefOneSpecified (
74
+ Resources . FormatAnchorTagHelper_CannotOverrideSpecifiedHref (
74
75
"<a>" ,
75
- nameof ( Action ) ,
76
- nameof ( Controller ) ,
77
- nameof ( Route ) ,
76
+ nameof ( Action ) . ToLowerInvariant ( ) ,
77
+ nameof ( Controller ) . ToLowerInvariant ( ) ,
78
+ nameof ( Route ) . ToLowerInvariant ( ) ,
78
79
RouteAttributePrefix ,
79
80
Href ) ) ;
80
81
}
@@ -85,6 +86,7 @@ public override void Process(TagHelperContext context, TagHelperOutput output)
85
86
else
86
87
{
87
88
TagBuilder tagBuilder ;
89
+ var routeValues = GetRouteValues ( output , routePrefixedAttributes ) ;
88
90
89
91
if ( Route == null )
90
92
{
@@ -103,9 +105,9 @@ public override void Process(TagHelperContext context, TagHelperOutput output)
103
105
throw new InvalidOperationException (
104
106
Resources . FormatAnchorTagHelper_CannotDetermineHrefRouteActionOrControllerSpecified (
105
107
"<a>" ,
106
- nameof ( Route ) ,
107
- nameof ( Action ) ,
108
- nameof ( Controller ) ,
108
+ nameof ( Route ) . ToLowerInvariant ( ) ,
109
+ nameof ( Action ) . ToLowerInvariant ( ) ,
110
+ nameof ( Controller ) . ToLowerInvariant ( ) ,
109
111
Href ) ) ;
110
112
}
111
113
else
@@ -126,41 +128,41 @@ public override void Process(TagHelperContext context, TagHelperOutput output)
126
128
}
127
129
}
128
130
129
- private static Dictionary < string , object > GetRouteValues ( TagHelperOutput output )
131
+ // TODO: We will not need this method once https://github.com/aspnet/Razor/issues/89 is completed.
132
+ private static Dictionary < string , object > GetRouteValues (
133
+ TagHelperOutput output , IEnumerable < KeyValuePair < string , string > > routePrefixedAttributes )
130
134
{
131
- var prefixedValues = output . PullPrefixedAttributes ( RouteAttributePrefix ) ;
132
-
133
135
Dictionary < string , object > routeValues = null ;
134
-
135
- if ( prefixedValues . Any ( ) )
136
+ if ( routePrefixedAttributes . Any ( ) )
136
137
{
137
- // Generator.GenerateActionLink || GenerateRouteLink does not accept a Dictionary<string, string> for
138
- // route values.
139
- routeValues = prefixedValues . ToDictionary (
138
+ // Prefixed values should be treated as bound attributes, remove them from the output.
139
+ output . RemoveRange ( routePrefixedAttributes ) ;
140
+
141
+ // Generator.GenerateForm does not accept a Dictionary<string, string> for route values.
142
+ routeValues = routePrefixedAttributes . ToDictionary (
140
143
attribute => attribute . Key . Substring ( RouteAttributePrefix . Length ) ,
141
144
attribute => ( object ) attribute . Value ) ;
142
145
}
143
146
144
147
return routeValues ;
145
148
}
146
149
147
-
148
150
// Restores bound HTML attributes when we detect that a user is using the AnchorTagHelper as a normal <a> tag.
149
151
private void RestoreBoundHtmlAttributes ( TagHelperContext context , TagHelperOutput output )
150
152
{
151
153
if ( Protocol != null )
152
154
{
153
- output . RestoreBoundHtmlAttribute ( nameof ( Protocol ) , context ) ;
155
+ output . CopyHtmlAttribute ( nameof ( Protocol ) , context ) ;
154
156
}
155
157
156
158
if ( Host != null )
157
159
{
158
- output . RestoreBoundHtmlAttribute ( nameof ( Host ) , context ) ;
160
+ output . CopyHtmlAttribute ( nameof ( Host ) , context ) ;
159
161
}
160
162
161
163
if ( Fragment != null )
162
164
{
163
- output . RestoreBoundHtmlAttribute ( nameof ( Fragment ) , context ) ;
165
+ output . CopyHtmlAttribute ( nameof ( Fragment ) , context ) ;
164
166
}
165
167
}
166
168
}
0 commit comments