最近做的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?! ?/p>
[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.49028c.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詳細變化見這里。
新聞熱點
疑難解答