最近做的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详细变化见这里

ASP.NET 5 Beta5 对TagHelper带来的变化的更多相关文章

  1. ASP.NET 5 Beta5来了(翻译)

    在6月30日微软发布了ASP.NET 5 Beta5,我们可以从http://nuget.org上获取Beta5 的packages. 随着VS2015RC发布的ASP.NET 5的版本号是Beta4 ...

  2. .NET跨平台之旅:将示例站点从ASP.NET 5 Beta5升级至Beta7

    9月2日,微软发布了ASP.NET 5 Beta7(详见Announcing Availability of ASP.NET 5 Beta7).其中最大的亮点是dnx已经可以完全基于CoreCLR运行 ...

  3. .NET跨平台之旅:借助ASP.NET 5 Beta5的新特性显示CLR与操作系统信息

    今天在 MSDN 博客上看到了 ASP.NET 5 Beta5 的发布消息(详见 ASP.NET 5 Beta5 Now Available),从中知道了 2 个新特性: 1. DNX: New IR ...

  4. ASP.NET MVC Core的TagHelper (高级特性)

    这篇博文ASP.NET MVC Core的TagHelper(基础篇)介绍了TagHelper的基本概念和创建自定义TagHelper的方式,接着继续介绍一些新的看起来比较高级的特性.(示例代码紧接着 ...

  5. [翻译] 初看 ASP.NET Core 3.0 即将到来的变化

    [翻译] 初看 ASP.NET Core 3.0 即将到来的变化 原文: A first look at changes coming in ASP.NET Core 3.0 在我们努力完成下一个 m ...

  6. 【重构前端知识体系之HTML】HTML5给网页音频带来的变化

    [重构前端知识体系之HTML]HTML5给网页音频带来的变化 引言 音乐播放,相信大家都很熟悉,但是早在之前的音乐播放之前,你的浏览器会问你,是否下载flash插件.然而现在,估计一些年轻的开发者都不 ...

  7. asp.net core高级应用:TagHelper+Form

    上一篇博客我讲解了TagHelper的基本用法和自定义标签的生成,那么我就趁热打铁,和大家分享一下TagHelper的高级用法~~,大家也可以在我的博客下随意留言. 对于初步接触asp.net cor ...

  8. ASP.NET MVC Core的TagHelper(基础篇)

    TagHelper又是一个新的名词,它替代了自之前MVC版本的HtmlHelper,专注于在cshmlt中辅助生成html标记. 通过使用自定义的TagHelper可以提供自定义的Html属性或元素, ...

  9. asp.net core razor自定义taghelper

    又一个新的名词(taghelper),这个名词在netcore razor中也替代了(Htmlhelper),通过taghelper是可以操作html标签.条件输出.更是自由添加内外元素.当然也内置了 ...

随机推荐

  1. 刽子手游戏(UVa489)

    题目具体描述见:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_prob ...

  2. HTK训练错误消息意义

    在HTK训练线上数据的时候,遇到了ERROR [+6550] LoadHTKLabels: Junk at end of HTK transcription,这个问题,网上查阅是说有空行,结果根本没有 ...

  3. Python并发编-用Event,线程检测数据库连接的例子

    尝试3次连接数据库 import time import random from threading import Thread,Event def connect_db(e): count = 0 ...

  4. 11.6八校联考T1,T2题解

    因为版权问题,不丢题面,不放代码了(出题人姓名也隐藏) T1 这,是一道,DP题,但是我最开始看的时候,我思路挂了,以为是一道简单题,然后就写错了 后来,我正确理解题意后写了个dfs,幸亏没有记忆化, ...

  5. Contains,Exists,Any,Count 比较是否存在某个元素

    private static void Main(string[] args) { ; Console.WriteLine("判断是否存在某个元素 :"); Console.Wri ...

  6. Xamarin 2017.9.13发布更新

     Xamarin 2017.9.13发布更新 Visual Studio 2017更新到15.3.4.其中,Xamarin.Visual更新到4.6.3.4;Xamarin.iOS更新到10.12.3 ...

  7. luoguP3830 [SHOI2012]随机树 期望概率 + 动态规划 + 结论

    题意非常的复杂,考虑转化一下: 每次选择一个叶节点,删除本叶节点(深度为$dep$)的同时,加入两个深度为$dep + 1$的叶节点,重复$n$轮 首先考虑第$1$问,(你看我这种人相信数据绝对是最大 ...

  8. [BZOJ4627][BeiJing2016]回转寿司(线段树)

    从左到右处理,设到当前数R的前缀和为cnt[i],则以i为右端点的合法的区间左端点j必然是L<=cnt[i]-cnt[j-1]<=R,即cnt[i]-R<=cnt[j-1]<= ...

  9. POJ3744 Scout YYF I 概率DP+矩阵快速幂

    http://poj.org/problem?id=3744 题意:一条路,起点为1,有概率p走一步,概率1-p跳过一格(不走中间格的走两步),有n个点不能走,问到达终点(即最后一个坏点后)不踩坏点的 ...

  10. Request Response 跳转页面的理解

    1.response 跳转页面 private void writeContent(String content) { HttpServletResponse response = ((Servlet ...