-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Added default UrlResolutionTagHelper
to resolve app relative URLs.
#2875
Conversation
/cc @dougbu @ajaybhargavb |
{ | ||
return null; | ||
} | ||
|
||
return GenerateClientUrl(_httpContext.Request.PathBase, contentPath); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure if we need to fix this here, but one of the reasons for #2841 is that GenerateClientUrl
isn't trimming the contentPath
so a leading space will result in ~
not being removed.
Do we want to fix that here or leaving the trimming up to caller?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, part of me feels like it'd be too protective over users; but then again I can't imagine a user purposefully calling into this method with an untrimmed value.
I'm not sure change this method directly would be the right approach; however, it brings up some gray area in the UrlResolutionTagHelper
where it is smarter about parsing URLs with whitespace where our ScriptTagHelper
, LinkTagHelper
and ImageTagHelper
take a users value verbatim and hand it over to the IUrlHelper
.
⌚ |
ee91fee
to
bd29dd9
Compare
Updated. |
if (TryResolveUrl( | ||
(string)attribute.Value, | ||
tryEncodeApplicationPath: false, | ||
resolvedUrl: out resolvedUrl)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed one bit of "bit off" formatting but not the other...
Looks like some tests broke after rebasing, fixed them and the formatting issue. |
/// <see cref="ITagHelper"/> implementation targeting elements containing attributes with URL expected values. | ||
/// </summary> | ||
/// <remarks>Resolves application relative URLs that are not targeted by other <see cref="ITagHelper"/>s. Runs | ||
/// prior to other <see cref="ITagHelper"/>s to ensure application-relative URLs are resolved.</remarks> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As discussed elsewhere, "application relative" is not consistent with how that term is used elsewhere. But also have no existing doc comments hyphenating "application-relative".
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This doesn't matter.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here "Resolves application relative URLs" -> "Resolves URLs relative to the application's 'webroot' setting" or (perhaps better) "Resolves URLs starting with '~/' (relative to the application's 'webroot' setting)".
⌚ |
@@ -195,4 +195,9 @@ | |||
<data name="RazorPage_InvalidTagHelperIndexerAssignment" xml:space="preserve"> | |||
<value>Unable to perform '{0}' assignment. Tag helper property '{1}.{2}' must not be null.</value> | |||
</data> | |||
<data name="CouldNotResolveApplicationRelativeUrl_TagHelper" xml:space="preserve"> | |||
<value>Unexpected return value from '{1}.{2}' for URL '{0}'. If you have overridden the '{1}' service, change '{2}' to replace only the '~/' prefix. Otherwise, add the following directive to your Razor page to disable application-relative URL resolution: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Error messages shouldn't assign blame. Remove all the "you" stuff and make it objective: "If the blah service has been overridden... blah blah. Add the Foo directive to the Razor page...."
3401afe
to
61a0884
Compare
Updated. |
@@ -195,4 +195,9 @@ | |||
<data name="RazorPage_InvalidTagHelperIndexerAssignment" xml:space="preserve"> | |||
<value>Unable to perform '{0}' assignment. Tag helper property '{1}.{2}' must not be null.</value> | |||
</data> | |||
<data name="CouldNotResolveApplicationRelativeUrl_TagHelper" xml:space="preserve"> | |||
<value>Unexpected return value from '{1}.{2}' for URL '{0}'. If the '{1}' service has been overridden, change '{2}' to replace only the '~/' prefix. Otherwise, add the following directive to the Razor page to disable application-relative URL resolution: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Discussed the now-hidden thread with @Eilon offline. Decided to avoid using "application-relative" in a new way.
Here "application-relative URL resolution" -> "URL resolution relative to the application's 'webroot' setting". Might want to pass "webroot" into the resource.
⌚ |
61a0884
to
c2d6bc7
Compare
Updated. |
/// <summary> | ||
/// Resolves and updates URL values relative to the application's 'webroot' setting (values starting with '~/') | ||
/// for <paramref name="output"/>'s <see cref="TagHelperOutput.Attributes"/> whose | ||
/// <see cref="TagHelperAttribute.Name"/> is <paramref name="attributeName"/>. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wording in class' <remarks>
are clearer e.g. Resolves and updates URL values starting with '~/' (relative to the application's 'webroot' setting) for ...
.
Fix #2903 in this PR. As discussed in a hidden thread, currently doing extra work to preserve a bug. |
⌚ |
c2d6bc7
to
dd048d2
Compare
Updated. |
Let's not add more to this PR at this point. It's fine to treat other items as bugs. |
/// <param name="encodeWebRoot">If <c>true</c>, will encode the path to the application's root defined by the | ||
/// 'webroot' setting.</param> | ||
/// <param name="resolvedUrl">The URL value relative to the application's 'webroot' setting.</param> | ||
/// <returns><c>true</c> if the <paramref name="url"/> could be resolved; <c>false</c> otherwise.</returns> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry for commenting on a commit (amateur move). My comments were on this set of doc comments.
|
- Razor removed the ability to automatically resolve URLs prefixed with `~/`; therefore `ScriptTagHelper`, `LinkTagHelper` and `ImageTagHelper` have changed to take in `IUrlHelper`s and auto-resolve their URL based properties if they start with `~/`. - Added a catch-all `~/` resolver for non `TagHelper` based HTML elements. Razor used to resolve any attribute value that started with `~/` now the behavior is restricted to attributes that can contain URLs. - Updated `IUrlHelper` to accept `null` values. - Added functional tests to validate that URLs resolve correctly. - Updated `TagHelper` tests to ensure that URLs are resolved via an `IUrlHelper`. #2807
dd048d2
to
0ef68ee
Compare
~/
; thereforeScriptTagHelper
,LinkTagHelper
andImageTagHelper
have changed to take inIUrlHelper
s and auto-resolve their URL based properties if they start with~/
.~/
resolver for nonTagHelper
based HTML elements. Razor used to resolve any attribute value that started with~/
now the behavior is restricted to attributes that can contain URLs.IUrlHelper
to acceptnull
values.TagHelper
tests to ensure that URLs are resolved via anIUrlHelper
.Add URL resolving TagHelper to replace Razor ~/ functionality. #2807
Razor PR to remove existing app-relative URL resolution: aspnet/Razor#466