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

Commit

Permalink
Added default UrlResolutionTagHelper to resolve app relative URLs.
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
NTaylorMullen committed Aug 3, 2015
1 parent 6d228a6 commit 0ef68ee
Show file tree
Hide file tree
Showing 23 changed files with 831 additions and 81 deletions.
7 changes: 6 additions & 1 deletion src/Microsoft.AspNet.Mvc.Core/UrlHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,13 @@ protected virtual string GeneratePathFromRoute(string routeName, IDictionary<str
}

/// <inheritdoc />
public virtual string Content([NotNull] string contentPath)
public virtual string Content(string contentPath)
{
if (contentPath == null)
{
return null;
}

return GenerateClientUrl(_httpContext.Request.PathBase, contentPath);
}

Expand Down
7 changes: 5 additions & 2 deletions src/Microsoft.AspNet.Mvc.Razor.Host/MvcRazorHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,16 @@ public class MvcRazorHost : RazorEngineHost, IMvcRazorHost
"Microsoft.AspNet.Mvc",
"Microsoft.AspNet.Mvc.Rendering",
};
private static readonly Chunk[] _defaultInheritedChunks = new[]
private static readonly Chunk[] _defaultInheritedChunks = new Chunk[]
{
new InjectChunk("Microsoft.AspNet.Mvc.Rendering.IHtmlHelper<TModel>", HtmlHelperPropertyName),
new InjectChunk("Microsoft.AspNet.Mvc.Rendering.IJsonHelper", "Json"),
new InjectChunk("Microsoft.AspNet.Mvc.IViewComponentHelper", "Component"),
new InjectChunk("Microsoft.AspNet.Mvc.IUrlHelper", "Url"),
new AddTagHelperChunk
{
LookupText = "Microsoft.AspNet.Mvc.Razor.TagHelpers.UrlResolutionTagHelper, Microsoft.AspNet.Mvc.Razor"
},
};

// CodeGenerationContext.DefaultBaseClass is set to MyBaseType<dynamic>.
Expand Down Expand Up @@ -99,7 +103,6 @@ internal MvcRazorHost(IChunkTreeCache chunkTreeCache, RazorPathNormalizer pathNo
MarkAsHtmlEncodedMethodName = HtmlHelperPropertyName + ".Raw",
})
{
ResolveUrlMethodName = "Href",
BeginContextMethodName = "BeginContext",
EndContextMethodName = "EndContext"
};
Expand Down
20 changes: 20 additions & 0 deletions src/Microsoft.AspNet.Mvc.Razor/Properties/Resources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

59 changes: 32 additions & 27 deletions src/Microsoft.AspNet.Mvc.Razor/Resources.resx
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema
<!--
Microsoft ResX Schema
Version 2.0
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types.
Example:
... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader>
Expand All @@ -26,36 +26,36 @@
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
<comment>This is a comment</comment>
</data>
There are any number of "resheader" rows that contain simple
There are any number of "resheader" rows that contain simple
name/value pairs.
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
mimetype set.
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.
mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->
Expand Down Expand Up @@ -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 URL resolution relative to the application's 'webroot' setting:

@{3} "{4}, {5}"</value>
</data>
</root>
218 changes: 218 additions & 0 deletions src/Microsoft.AspNet.Mvc.Razor/TagHelpers/UrlResolutionTagHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,218 @@
// 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.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using Microsoft.AspNet.Mvc.Rendering;
using Microsoft.AspNet.Razor.Runtime.TagHelpers;
using Microsoft.Framework.WebEncoders;

namespace Microsoft.AspNet.Mvc.Razor.TagHelpers
{
/// <summary>
/// <see cref="ITagHelper"/> implementation targeting elements containing attributes with URL expected values.
/// </summary>
/// <remarks>Resolves URLs starting with '~/' (relative to the application's 'webroot' setting) 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>
[TargetElement("*", Attributes = "itemid")]
[TargetElement("a", Attributes = "href")]
[TargetElement("applet", Attributes = "archive")]
[TargetElement("area", Attributes = "href")]
[TargetElement("audio", Attributes = "src")]
[TargetElement("base", Attributes = "href")]
[TargetElement("blockquote", Attributes = "cite")]
[TargetElement("button", Attributes = "formaction")]
[TargetElement("del", Attributes = "cite")]
[TargetElement("embed", Attributes = "src")]
[TargetElement("form", Attributes = "action")]
[TargetElement("html", Attributes = "manifest")]
[TargetElement("iframe", Attributes = "src")]
[TargetElement("img", Attributes = "src")]
[TargetElement("input", Attributes = "src")]
[TargetElement("input", Attributes = "formaction")]
[TargetElement("ins", Attributes = "cite")]
[TargetElement("link", Attributes = "href")]
[TargetElement("menuitem", Attributes = "icon")]
[TargetElement("object", Attributes = "archive")]
[TargetElement("object", Attributes = "data")]
[TargetElement("q", Attributes = "cite")]
[TargetElement("script", Attributes = "src")]
[TargetElement("source", Attributes = "src")]
[TargetElement("track", Attributes = "src")]
[TargetElement("video", Attributes = "src")]
[TargetElement("video", Attributes = "poster")]
[EditorBrowsable(EditorBrowsableState.Never)]
public class UrlResolutionTagHelper : TagHelper
{
// Valid whitespace characters defined by the HTML5 spec.
private static readonly char[] ValidAttributeWhitespaceChars =
new[] { '\t', '\n', '\u000C', '\r', ' ' };
private static readonly IReadOnlyDictionary<string, IEnumerable<string>> ElementAttributeLookups =
new Dictionary<string, IEnumerable<string>>(StringComparer.OrdinalIgnoreCase)
{
{ "a", new[] { "href" } },
{ "applet", new[] { "archive" } },
{ "area", new[] { "href" } },
{ "audio", new[] { "src" } },
{ "base", new[] { "href" } },
{ "blockquote", new[] { "cite" } },
{ "button", new[] { "formaction" } },
{ "del", new[] { "cite" } },
{ "embed", new[] { "src" } },
{ "form", new[] { "action" } },
{ "html", new[] { "manifest" } },
{ "iframe", new[] { "src" } },
{ "img", new[] { "src" } },
{ "input", new[] { "src", "formaction" } },
{ "ins", new[] { "cite" } },
{ "link", new[] { "href" } },
{ "menuitem", new[] { "icon" } },
{ "object", new[] { "archive", "data" } },
{ "q", new[] { "cite" } },
{ "script", new[] { "src" } },
{ "source", new[] { "src" } },
{ "track", new[] { "src" } },
{ "video", new[] { "poster", "src" } },
};

/// <summary>
/// Creates a new <see cref="UrlResolutionTagHelper"/>.
/// </summary>
/// <param name="urlHelper">The <see cref="IUrlHelper"/>.</param>
/// <param name="htmlEncoder">The <see cref="IHtmlEncoder"/>.</param>
public UrlResolutionTagHelper(IUrlHelper urlHelper, IHtmlEncoder htmlEncoder)
{
UrlHelper = urlHelper;
HtmlEncoder = htmlEncoder;
}

/// <inheritdoc />
public override int Order
{
get
{
return DefaultOrder.DefaultFrameworkSortOrder - 999;
}
}

protected IUrlHelper UrlHelper { get; }

protected IHtmlEncoder HtmlEncoder { get; }

/// <inheritdoc />
public override void Process(TagHelperContext context, TagHelperOutput output)
{
IEnumerable<string> attributeNames;
if (ElementAttributeLookups.TryGetValue(output.TagName, out attributeNames))
{
foreach (var attributeName in attributeNames)
{
ProcessUrlAttribute(attributeName, output);
}
}

// itemid can be present on any HTML element.
ProcessUrlAttribute("itemid", output);
}

/// <summary>
/// Resolves and updates URL values starting with '~/' (relative to the application's 'webroot' setting) for
/// <paramref name="output"/>'s <see cref="TagHelperOutput.Attributes"/> whose
/// <see cref="TagHelperAttribute.Name"/> is <paramref name="attributeName"/>.
/// </summary>
/// <param name="attributeName">The attribute name used to lookup values to resolve.</param>
/// <param name="output">The <see cref="TagHelperOutput"/>.</param>
protected void ProcessUrlAttribute(string attributeName, TagHelperOutput output)
{
IEnumerable<TagHelperAttribute> attributes;
if (output.Attributes.TryGetAttributes(attributeName, out attributes))
{
foreach (var attribute in attributes)
{
string resolvedUrl;

var stringValue = attribute.Value as string;
if (stringValue != null)
{
if (TryResolveUrl(stringValue, encodeWebRoot: false, resolvedUrl: out resolvedUrl))
{
attribute.Value = resolvedUrl;
}
}
else
{
var htmlStringValue = attribute.Value as HtmlString;
if (htmlStringValue != null &&
TryResolveUrl(
htmlStringValue.ToString(),
encodeWebRoot: true,
resolvedUrl: out resolvedUrl))
{
attribute.Value = new HtmlString(resolvedUrl);
}
}
}
}
}

/// <summary>
/// Tries to resolve the given <paramref name="url"/> value relative to the application's 'webroot' setting.
/// </summary>
/// <param name="url">The URL to resolve.</param>
/// <param name="encodeWebRoot">If <c>true</c>, will HTML encode the expansion of '~/'.</param>
/// <param name="resolvedUrl">Absolute URL beginning with the application's virtual root. <c>null</c> if
/// <paramref name="url"/> could not be resolved.</param>
/// <returns><c>true</c> if the <paramref name="url"/> could be resolved; <c>false</c> otherwise.</returns>
protected bool TryResolveUrl(string url, bool encodeWebRoot, out string resolvedUrl)
{
resolvedUrl = null;

if (url == null)
{
return false;
}

var trimmedUrl = url.Trim(ValidAttributeWhitespaceChars);

// Before doing more work, ensure that the URL we're looking at is app relative.
if (trimmedUrl.Length >= 2 && trimmedUrl[0] == '~' && trimmedUrl[1] == '/')
{
var appRelativeUrl = UrlHelper.Content(trimmedUrl);

if (encodeWebRoot)
{
var postTildeSlashUrlValue = trimmedUrl.Substring(2);

if (!appRelativeUrl.EndsWith(postTildeSlashUrlValue, StringComparison.Ordinal))
{
throw new InvalidOperationException(
Resources.FormatCouldNotResolveApplicationRelativeUrl_TagHelper(
url,
nameof(IUrlHelper),
nameof(IUrlHelper.Content),
"removeTagHelper",
typeof(UrlResolutionTagHelper).FullName,
typeof(UrlResolutionTagHelper).GetTypeInfo().Assembly.GetName().Name));
}

var applicationPath = appRelativeUrl.Substring(0, appRelativeUrl.Length - postTildeSlashUrlValue.Length);
var encodedApplicationPath = HtmlEncoder.HtmlEncode(applicationPath);

resolvedUrl = string.Concat(encodedApplicationPath, postTildeSlashUrlValue);
}
else
{
resolvedUrl = appRelativeUrl;
}

return true;
}

return false;
}
}
}
Loading

0 comments on commit 0ef68ee

Please sign in to comment.