You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Classic Razor would resolve the value of any HTML attribute that started with ~/ as an application relative URL. This led to some invalid scenarios where users would provide attribute values such as ~/folder/file.txt and get inaccurate URL resolutions. In addition, this feature couldn't be easily disabled.
The Fix
There were multiple locations for a valid fix; we decided that it didn't make sense to hard code the list of HTML5 defined element/attribute combos in the core Razor parser. In addition, the introduction of TagHelpers led to some users expecting to see the raw value of their attribute values instead of the intercepted value.
Any element that the UrlResolutionTagHelper targets now have runtime TagHelper restrictions applied to them. Some of these restrictions are:
Depending on the targets TagStructure a tag targeted by the UrlResolutionTagHelper may result in an error based on supported syntax.
TagStructure.WithoutEndTag = <tag> OR <tag />
TagStructure.NormalOrSelfClosing = <tag></tag> OR <tag />
TagStructure.Unspecified = Usually same behavior as TagStructure.NormalOrSelfClosing unless another TagHelper defines a different TagStructure.
C# must not exist in the declaration area of the tag.
Invalid: <a href="..." @myClass>...</a>
Valid: <a href="..." class=@myClass>...</a>
TagHelpers that bind URL based attribute values must now resolve them manually.
Can be done by taking in the IUrlHelper in the constructor (from DI) and calling IUrlHelper.Content on the value.
Note: These restrictions do not show up during design time due to the editor browsable attribute on UrlResolutionTagHelper. Reason behind this decision was that the majority of users will never encounter any of these restrictions and the TagHelper tooling would be misleading.
Workarounds
Disable URL resolution on the page:
By default every page essentially has an: @addTagHelper "Microsoft.AspNet.Mvc.Razor.TagHelpers.UrlResolutionTagHelper, Microsoft.AspNet.Mvc.Razor"
Therefore, to disable ~/ URL resolution for an entire page you can do: @removeTagHelper "Microsoft.AspNet.Mvc.Razor.TagHelpers.UrlResolutionTagHelper, Microsoft.AspNet.Mvc.Razor"
This can be done on the page and there's an issue tracking the ability to do this in _ViewImports.cshtml.
Disable URL resolution on an element:
You can also disable the URL resolution capability on a per-element basis via the TagHelper opt-out character !: <!href href="~/..." @myAttribute>...</a>
The text was updated successfully, but these errors were encountered:
What's the Problem?
Classic Razor would resolve the value of any HTML attribute that started with
~/
as an application relative URL. This led to some invalid scenarios where users would provide attribute values such as~/folder/file.txt
and get inaccurate URL resolutions. In addition, this feature couldn't be easily disabled.The Fix
There were multiple locations for a valid fix; we decided that it didn't make sense to hard code the list of HTML5 defined element/attribute combos in the core Razor parser. In addition, the introduction of
TagHelper
s led to some users expecting to see the raw value of their attribute values instead of the intercepted value.Therefore, we decided to create the UrlResolutionTagHelper to take over
~/
resolution.It targets the following attribute/element combos:
<a>
,<area>
,<link>
,<base>
<audio>
,<embed>
,<iframe>
,<img>
,<input>
,<script>
,<source>
,<track>
,<video>
<form>
<blockquote>
,<del>
,<ins>
,<q>
<object>
<button>
,<input>
<menuitem>
<html>
<video>
<img>
,<source>
<object>
,<applet>
Side effects
Any element that the
UrlResolutionTagHelper
targets now have runtimeTagHelper
restrictions applied to them. Some of these restrictions are:TagStructure
a tag targeted by theUrlResolutionTagHelper
may result in an error based on supported syntax.TagStructure.WithoutEndTag
=<tag>
OR<tag />
TagStructure.NormalOrSelfClosing
=<tag></tag>
OR<tag />
TagStructure.Unspecified
= Usually same behavior asTagStructure.NormalOrSelfClosing
unless anotherTagHelper
defines a differentTagStructure
.<a href="..." @myClass>...</a>
<a href="..." class=@myClass>...</a>
TagHelper
s that bind URL based attribute values must now resolve them manually.IUrlHelper
in the constructor (from DI) and callingIUrlHelper.Content
on the value.Note: These restrictions do not show up during design time due to the editor browsable attribute on
UrlResolutionTagHelper
. Reason behind this decision was that the majority of users will never encounter any of these restrictions and theTagHelper
tooling would be misleading.Workarounds
Disable URL resolution on the page:
By default every page essentially has an:
@addTagHelper "Microsoft.AspNet.Mvc.Razor.TagHelpers.UrlResolutionTagHelper, Microsoft.AspNet.Mvc.Razor"
Therefore, to disable
~/
URL resolution for an entire page you can do:@removeTagHelper "Microsoft.AspNet.Mvc.Razor.TagHelpers.UrlResolutionTagHelper, Microsoft.AspNet.Mvc.Razor"
This can be done on the page and there's an issue tracking the ability to do this in
_ViewImports.cshtml
.Disable URL resolution on an element:
You can also disable the URL resolution capability on a per-element basis via the
TagHelper
opt-out character!
:<!href href="~/..." @myAttribute>...</a>
The text was updated successfully, but these errors were encountered: