最近作的TagHelper項目要從原來的ASP.NET 5 Beta 4升級到Beta 5,特意整理了升級後的變化:html
<img asp-file-version="true" src="~/images/my_cool_image.png" />
<a asp-action="Edit" asp-route-id="@index">Edit</a>
在該類中定義以下:git
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。 github
[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上支持的。
好比 json
<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; } }
如今:app
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的智能提示了。 asp.net
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:spa
<my-tag-helper> Content in source </my-tag-helper>
最後進過解析後生成到頁面的內容是:.net
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詳細變化見這裏。code