国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

ASP.NET 5 Beta5 對TagHelper帶來的變化

2019-11-17 02:12:49
字體:
供稿:網(wǎng)友

asp.net 5 Beta5 對TagHelper帶來的變化

最近做的TagHelper項(xiàng)目要從原來的ASP.NET 5 Beta 4升級到Beta 5,特地整理了升級后的變化:

  1. 新增ImageTagHelper
    <img asp-file-version="true" src="~/images/my_cool_image.png" /> 
  2. Tag Helper支持綁定字典屬性現(xiàn)在你可以在TagHelpers中綁定服務(wù)器端的attributes到字典屬性。比如,AnchorTagHelper利用名字格式為asp-route-*的attributes來設(shè)置路由值。
    <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屬性相關(guān)的定義,主要是在該屬性頭上添加HtmlAttributeName并設(shè)置其DictionaryAttributePrefix。  

  3. Tag Helper支持基于服務(wù)端Attributes設(shè)置的條件綁定,并支持通配符*。你可以利用TargetElementAttribute中Attributes屬性來指定當(dāng)前TagHelper應(yīng)用到擁有某些attributes的tag上。比如AnchorTagHelper類的定義如下:
    [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會應(yīng)用到A tag上,并且這個(gè)tag上需要有asp-action, asp-controller, asp-fragment, asp-host, asp-protocol, asp-route, asp-all-route-data和asp-route-*這些attributes中一個(gè)或一個(gè)以上,否則該tag就會綁定到該TagHelper。在最后一個(gè)條件綁定中,使用了通配符*,這也是Beta5上支持的。比如  

    <a href="http://m.survivalescaperooms.com/liontone/">上善若水</a>

    就不會被應(yīng)用上AnchorTagHelper。

  4. 移除Activate attribute。以前:
    public class MyTagHelper : TagHelper{    [HtmlAttributeNotBound]    [Activate]    public IHtmlEncoder Encoder { get; set; }    [HtmlAttributeNotBound]    [Activate]    public ViewContext ViewContext { get; set; }}

    現(xiàn)在:

    public class MyTagHelper : TagHelper{    public MyTagHelper(IHtmlEncoder encoder)    {        Encoder = encoder;    }    public IHtmlEncoder Encoder { get; }    [HtmlAttributeNotBound]    [ViewContext]    public ViewContext ViewContext { get; set; }}
  5. 不允許attribute名為"data-*"。在beta5中attribute名不能以"data-"開頭,不然在解析taghelper時(shí)就會有錯(cuò)誤拋出。
  6. 新增HtmlAttributeNotBoundAttribute,可以類中公開的屬性不轉(zhuǎn)化為TagHelper的Attribute。詳細(xì)介紹見這里。比如
    public class MyTagHelper : TagHelper{    public MyTagHelper(IHtmlEncoder encoder)    {        Encoder = encoder;    }    public IHtmlEncoder Encoder { get; }    [HtmlAttributeNotBound]    [ViewContext]    public ViewContext ViewContext { get; set; }}

    按照以前文章介紹,ViewContext對應(yīng)TagHelper的Attribute是view-context,但其實(shí)我們不希望它成為Attribute,這時(shí)只需要加上HtmlAttributeNotBoundAttribute即可,在Visual Studio 2015中也不會有該Attribute的智能提示了。  

  7. 程序集中內(nèi)嵌資源key已經(jīng)回歸到asp.net 5之前的樣子即namespace + "." + 文件名在beta4中key是與文件相對路徑基本一致,這一點(diǎn)比較另類,也許微軟自己也發(fā)現(xiàn)了這個(gè)問題,到了beta5又回歸到以前那樣,key的生成是文件的namespace + "." + 文件名。
  8. TagHelperOutput新增了2個(gè)新的屬性:PreElement和PostElement。不同于PreContent和PostContent,利用這兩個(gè)屬性可以輸出內(nèi)容到當(dāng)前Tag的前面或后面,大家可以查看Github上的相應(yīng)的issue來了解更多信息。比如在類中重寫方法:
    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>

    最后進(jìn)過解析后生成到頁面的內(nèi)容是:

    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 
  9. project.json中preprocess默認(rèn)路徑是compiler/preprocess/**/*.cs,這里文件夾的名稱是小寫,如果程序中是其中文件夾名大寫的話,里面的代碼不會被執(zhí)行。原來我項(xiàng)目中對應(yīng)的文件夾都是大寫Compiler/Preprocess,在做beta4升級到beta5支持時(shí),發(fā)現(xiàn)里面的代碼沒有被執(zhí)行,后來試著在project.json中直接設(shè)置preprocess為Compiler/Preprocess/**/*.cs,這樣是可以的。但是想著既然文檔中說默認(rèn)路徑就是這個(gè)地址,為什么還要設(shè)置呢?就在百思不得其解的時(shí)候,突然發(fā)現(xiàn)它上面寫的都是小寫,于是試著把文件夾改成小寫,果然可以。真是大坑啊,如果文件夾大小寫敏感,那一開始就要嚴(yán)格要求。還不知道正式版發(fā)布后會是什么情況,拭目以待吧。
  10. Beta5版的Core的庫越來越完善,新增了Hashtable, Arraylist類之前beta4版本Core庫中是沒有這兩個(gè)類的定義,項(xiàng)目恰好又用到了,那只好自己添加了類,現(xiàn)在升級到beta5后,又有了,于是就將原來的刪除掉了。隨著正式版的臨近,core庫也會變得越來越完善。
  11. 有些庫名稱,類的namespace發(fā)生變化,比如:
    • 命名空間從Microsoft.Framework.ConfigurationModel 變成Microsoft.Framework.Configuration
    • Microsoft.Framework.ConfigurationModel.Json庫更名為Microsoft.Framework.Configuration.Json
    • 移除命名空間Microsoft.AspNet.Http.Core
    • 移除EntityFramewor庫,使用EntityFramework.SqlServer庫來代替。
    • 接口ICompileModule中定義的方法參數(shù)類型發(fā)生變化:類BeforeCompileContext代替接口IBeforeCompileContext,類AfterCompileContext代替接口IAfterCompileContext。還有其他的一些變化,這里也就沒有一一列出來,大家在實(shí)際升級過程中根據(jù)自己項(xiàng)目情況做相應(yīng)的修改。
  12. 發(fā)現(xiàn)了Beta5的一些已知問題,比如:在render section中如果使用AnchorTagHelper,在運(yùn)行的時(shí)候就會出錯(cuò),對應(yīng)的issue報(bào)在這里,這時(shí)候我不得不使用AnchorTagHelper,用html element代替它,據(jù)說在beta6中已經(jīng)修正了。

  上面是我在項(xiàng)目升級過程中遇到的問題,其實(shí)還有很多變化,需要大家根據(jù)自己項(xiàng)目情況來發(fā)現(xiàn),具體beta5詳細(xì)變化見這里。


發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 彭山县| 岐山县| 通城县| 永安市| 平阳县| 南木林县| 万年县| 宁化县| 屏东县| 屏南县| 杨浦区| 古丈县| 桓台县| 枣强县| 天峻县| 治县。| 黑河市| 仁布县| 古田县| 商丘市| 高雄县| 句容市| 临沂市| 大悟县| 泸定县| 通江县| 板桥市| 同心县| 信宜市| 兰溪市| 奉化市| 浦北县| 郯城县| 潞西市| 江门市| 社旗县| 陵川县| 东台市| 和龙市| 永安市| 闻喜县|