标签:
最近做的TagHelper项目要从原来的ASP.NET 5 Beta 4升级到Beta 5,特地整理了升级后的变化:
<img asp-file-version="true" src="~/images/my_cool_image.png" />
<a asp-action="Edit" asp-route-id="@index">Edit</a>
在该类中定义如下:
public class AnchorTagHelper : TagHelper
{
private const string RouteValuesDictionaryName = "asp-all-route-data";
private const string RouteValuesPrefix = "asp-route-";
/// <summary>
/// Additional parameters for the route.
/// </summary>
[HtmlAttributeName(RouteValuesDictionaryName, DictionaryAttributePrefix = RouteValuesPrefix)]
public IDictionary<string, string> RouteValues { get; set; } =
new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
...
}
这里只列出与该Dictionary属性相关的定义,主要是在该属性头上添加HtmlAttributeName并设置其DictionaryAttributePrefix。
[TargetElement("a", Attributes = ActionAttributeName)]
[TargetElement("a", Attributes = ControllerAttributeName)]
[TargetElement("a", Attributes = FragmentAttributeName)]
[TargetElement("a", Attributes = HostAttributeName)]
[TargetElement("a", Attributes = ProtocolAttributeName)]
[TargetElement("a", Attributes = RouteAttributeName)]
[TargetElement("a", Attributes = RouteValuesDictionaryName)]
[TargetElement("a", Attributes = RouteValuesPrefix + "*")]
public class AnchorTagHelper : TagHelper
{
private const string ActionAttributeName = "asp-action";
private const string ControllerAttributeName = "asp-controller";
private const string FragmentAttributeName = "asp-fragment";
private const string HostAttributeName = "asp-host";
private const string ProtocolAttributeName = "asp-protocol";
private const string RouteAttributeName = "asp-route";
private const string RouteValuesDictionaryName = "asp-all-route-data";
private const string RouteValuesPrefix = "asp-route-";
private const string Href = "href";
...
}
从上面可以看出,该TagHelper会应用到A tag上,并且这个tag上需要有asp-action, asp-controller, asp-fragment, asp-host, asp-protocol, asp-route, asp-all-route-data和asp-route-*这些attributes中一个或一个以上,否则该tag就会绑定到该TagHelper。在最后一个条件绑定中,使用了通配符*,这也是Beta5上支持的。
比如
<a href="http://www.cnblogs.com/liontone/">上善若水</a>
就不会被应用上AnchorTagHelper。
public class MyTagHelper : TagHelper
{
[HtmlAttributeNotBound]
[Activate]
public IHtmlEncoder Encoder { get; set; }
[HtmlAttributeNotBound]
[Activate]
public ViewContext ViewContext { get; set; }
}
现在:
public class MyTagHelper : TagHelper
{
public MyTagHelper(IHtmlEncoder encoder)
{
Encoder = encoder;
}
public IHtmlEncoder Encoder { get; }
[HtmlAttributeNotBound]
[ViewContext]
public ViewContext ViewContext { get; set; }
}
public class MyTagHelper : TagHelper
{
public MyTagHelper(IHtmlEncoder encoder)
{
Encoder = encoder;
}
public IHtmlEncoder Encoder { get; }
[HtmlAttributeNotBound]
[ViewContext]
public ViewContext ViewContext { get; set; }
}
按照以前文章介绍,ViewContext对应TagHelper的Attribute是view-context,但其实我们不希望它成为Attribute,这时只需要加上HtmlAttributeNotBoundAttribute即可,在Visual Studio 2015中也不会有该Attribute的智能提示了。
public void Process(TagHelperContext context, TagHelperOutput output)
{
var nl = Environment.NewLine;
var br = "<br />" + nl;
output.PreElement.Append("This will appear before source element" + br);
output.PreContent.Append(nl + "This will appear before source content" + br);
output.PostContent.Append(br + "This will appear after source content" + nl);
output.PostElement.Append(br + "This will appear after source element");
}
在View上TagHelper:
<my-tag-helper>
Content in source
</my-tag-helper>
最后进过解析后生成到页面的内容是:
This will appear before source element<br />
<my-tag-helper>
This will appear before source content<br />
Content in source<br />
This will appear after source content
</my-tag-helper><br />
This will appear after source element
上面是我在项目升级过程中遇到的问题,其实还有很多变化,需要大家根据自己项目情况来发现,具体beta5详细变化见这里。
ASP.NET 5 Beta5 对TagHelper带来的变化
标签:
原文地址:http://www.cnblogs.com/liontone/p/4669046.html