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

Added default UrlResolutionTagHelper to resolve app relative URLs. #2875

Merged
merged 1 commit into from
Aug 3, 2015

Conversation

NTaylorMullen
Copy link
Contributor

  • Razor removed the ability to automatically resolve URLs prefixed with ~/; therefore ScriptTagHelper, LinkTagHelper and ImageTagHelper have changed to take in IUrlHelpers 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.
    Add URL resolving TagHelper to replace Razor ~/ functionality. #2807

Razor PR to remove existing app-relative URL resolution: aspnet/Razor#466

@NTaylorMullen
Copy link
Contributor Author

/cc @dougbu @ajaybhargavb

{
return null;
}

return GenerateClientUrl(_httpContext.Request.PathBase, contentPath);
Copy link
Contributor

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?

Copy link
Contributor Author

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.

@dougbu
Copy link
Member

dougbu commented Jul 29, 2015

@NTaylorMullen NTaylorMullen force-pushed the nimullen/urlresolutionth.2807 branch from ee91fee to bd29dd9 Compare July 30, 2015 19:55
@NTaylorMullen
Copy link
Contributor Author

Updated.

if (TryResolveUrl(
(string)attribute.Value,
tryEncodeApplicationPath: false,
resolvedUrl: out resolvedUrl))
Copy link
Member

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...

@NTaylorMullen
Copy link
Contributor Author

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>
Copy link
Member

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".

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't matter.

Copy link
Member

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)".

@dougbu
Copy link
Member

dougbu commented Aug 2, 2015

@@ -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:
Copy link
Member

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...."

@NTaylorMullen NTaylorMullen force-pushed the nimullen/urlresolutionth.2807 branch from 3401afe to 61a0884 Compare August 3, 2015 06:16
@NTaylorMullen
Copy link
Contributor Author

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:
Copy link
Member

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.

@dougbu
Copy link
Member

dougbu commented Aug 3, 2015

@NTaylorMullen NTaylorMullen force-pushed the nimullen/urlresolutionth.2807 branch from 61a0884 to c2d6bc7 Compare August 3, 2015 21:57
@NTaylorMullen
Copy link
Contributor Author

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"/>.
Copy link
Member

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 ....

@dougbu
Copy link
Member

dougbu commented Aug 3, 2015

Fix #2903 in this PR. As discussed in a hidden thread, currently doing extra work to preserve a bug.

@dougbu
Copy link
Member

dougbu commented Aug 3, 2015

@NTaylorMullen NTaylorMullen force-pushed the nimullen/urlresolutionth.2807 branch from c2d6bc7 to dd048d2 Compare August 3, 2015 22:41
@NTaylorMullen
Copy link
Contributor Author

Updated.

@Eilon
Copy link
Member

Eilon commented Aug 3, 2015

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>
Copy link
Member

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.

@dougbu
Copy link
Member

dougbu commented Aug 3, 2015

:shipit: w/ last couple of doc comment fixes

- 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
@NTaylorMullen NTaylorMullen force-pushed the nimullen/urlresolutionth.2807 branch from dd048d2 to 0ef68ee Compare August 3, 2015 23:18
@NTaylorMullen NTaylorMullen merged commit 0ef68ee into dev Aug 3, 2015
@NTaylorMullen NTaylorMullen deleted the nimullen/urlresolutionth.2807 branch August 3, 2015 23:19
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants