ASP.NET 5 Beta5 对TagHelper带来的变化

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

  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>
    

    在该类中定义以下: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

  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上支持的。
    好比  json

    <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; }
    }
    

    如今:app

    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的智能提示了。  asp.net

  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: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 
  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详细变化见这里code

相关文章
相关标签/搜索