zoukankan      html  css  js  c++  java
  • ASP.NET 5 Beta5 对TagHelper带来的变化

    最近做的TagHelper项目要从原来的ASP.NET 5 Beta 4升级到Beta 5,特地整理了升级后的变化:

    1. 新增ImageTagHelper
      <img asp-file-version="true" src="~/images/my_cool_image.png" /> 
    2. Tag Helper支持绑定字典属性
      现在你可以在TagHelpers中绑定服务器端的attributes到字典属性。比如,AnchorTagHelper利用名字格式为asp-route-*的attributes来设置路由值。
      <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。  

    3. Tag Helper支持基于服务端Attributes设置的条件绑定,并支持通配符*。
      你可以利用TargetElementAttribute中Attributes属性来指定当前TagHelper应用到拥有某些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会应用到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。

    4. 移除Activate attribute
      以前:
      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; }
      }
    5. 不允许attribute名为"data-*"
      在beta5中attribute名不能以"data-"开头,不然在解析taghelper时就会有错误抛出。
    6. 新增HtmlAttributeNotBoundAttribute,可以类中公开的属性不转化为TagHelper的Attribute。
      详细介绍见这里
      比如
      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的智能提示了。  

    7. 程序集中内嵌资源key已经回归到asp.net 5之前的样子即namespace + "." + 文件名
      在beta4中key是与文件相对路径基本一致,这一点比较另类,也许微软自己也发现了这个问题,到了beta5又回归到以前那样,key的生成是文件的namespace + "." + 文件名。
    8. TagHelperOutput新增了2个新的属性:PreElement和PostElement。
      不同于PreContent和PostContent,利用这两个属性可以输出内容到当前Tag的前面或后面,大家可以查看Github上的相应的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>
      

      最后进过解析后生成到页面的内容是:

      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默认路径是compiler/preprocess/**/*.cs,这里文件夹的名称是小写,如果程序中是其中文件夹名大写的话,里面的代码不会被执行。
      原来我项目中对应的文件夹都是大写Compiler/Preprocess,在做beta4升级到beta5支持时,发现里面的代码没有被执行,后来试着在project.json中直接设置preprocess为Compiler/Preprocess/**/*.cs,这样是可以的。但是想着既然文档中说默认路径就是这个地址,为什么还要设置呢?就在百思不得其解的时候,突然发现它上面写的都是小写,于是试着把文件夹改成小写,果然可以。真是大坑啊,如果文件夹大小写敏感,那一开始就要严格要求。还不知道正式版发布后会是什么情况,拭目以待吧。
    10. Beta5版的Core的库越来越完善,新增了Hashtable, Arraylist类
      之前beta4版本Core库中是没有这两个类的定义,项目恰好又用到了,那只好自己添加了类,现在升级到beta5后,又有了,于是就将原来的删除掉了。随着正式版的临近,core库也会变得越来越完善。
    11. 有些库名称,类的namespace发生变化,比如:
      • 命名空间从Microsoft.Framework.ConfigurationModel 变成Microsoft.Framework.Configuration
      • Microsoft.Framework.ConfigurationModel.Json库更名为Microsoft.Framework.Configuration.Json
      • 移除命名空间Microsoft.AspNet.Http.Core
      • 移除EntityFramewor库,使用EntityFramework.SqlServer库来代替
      • 接口ICompileModule中定义的方法参数类型发生变化:类BeforeCompileContext代替接口IBeforeCompileContext,类AfterCompileContext代替接口IAfterCompileContext。
        还有其他的一些变化,这里也就没有一一列出来,大家在实际升级过程中根据自己项目情况做相应的修改。
    12. 发现了Beta5的一些已知问题,比如:
      在render section中如果使用AnchorTagHelper,在运行的时候就会出错,对应的issue报在这里,这时候我不得不使用AnchorTagHelper,用html element代替它,据说在beta6中已经修正了。

      上面是我在项目升级过程中遇到的问题,其实还有很多变化,需要大家根据自己项目情况来发现,具体beta5详细变化见这里

  • 相关阅读:
    甲午年总结
    浅谈数字营销
    机器学习笔记
    上海GDG活动有感
    我也谈谈游戏
    CSS3新增属性
    js事件详解
    DOM与BOM相关操作
    JS基础知识
    js数据类型
  • 原文地址:https://www.cnblogs.com/liontone/p/4669046.html
Copyright © 2011-2022 走看看