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

时间: 2024-08-29 13:08:34

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

ASP.NET 5 Beta5来了(翻译)

在6月30日微软发布了ASP.NET 5 Beta5,我们可以从http://nuget.org上获取Beta5 的packages. 随着VS2015RC发布的ASP.NET 5的版本号是Beta4,所以你一定想在你的项目里使用这个更新.Beta5上不但修正了之前的一些问题并有了很多改进,而且还新增了很多功能. 很重要一点,我们应该知道有ASP.NET的运行时(用来运行你的网络应用)和Visual Studio上Web 工具(比如HTML.JavaScript编辑器和新文件对话框).Beta5

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

这篇博文ASP.NET MVC Core的TagHelper(基础篇)介绍了TagHelper的基本概念和创建自定义TagHelper的方式,接着继续介绍一些新的看起来比较高级的特性.(示例代码紧接着上一遍博文) 一.使用自定义的标记元素 之前基础篇介绍的TagHelper的功能是给已有的HTML元素提供一个自定义的属性标记,然后服务器认出这个标记后,将标记转化成最终的HTML.这里将要介绍的功能是,定义个全新的Tag,看起来跟普通的HTML元素一样.是不是觉得很熟悉呢(前提是你用过Angula

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

9月2日,微软发布了ASP.NET 5 Beta7(详见Announcing Availability of ASP.NET 5 Beta7).其中最大的亮点是dnx已经可以完全基于CoreCLR运行,也就是在Mac/Linux上运行dnx无需再借助Mono,而之前必须安装Mono,才能运行dnu restore安装nuget包包. 我们的ASP.NET 5跨平台示例站点 about.cnblogs.com 之前是跑在 ASP.NET 5 Beta5 上的(详见借助ASP.NET 5 Beta5

asp.net core razor自定义taghelper

原文:asp.net core razor自定义taghelper 又一个新的名词(taghelper),通过taghelper是可以操作html标签.条件输出.更是自由添加内外元素.当然也内置了挺多的asp-开头的taghelper. 下面文章中也简单的带大家实现一个taghelper; 创建自定义html元素 创建一个类ButtonTagHelper tagName为标签名称,下面创建一个button标签 Copy using Microsoft.AspNetCore.Razor.TagHe

读再多的书都不如内生动力给人带来的变化大

读再多的书都不如内生动力给人带来的变化大 生活就是一个过程,应该要过得快乐一点,做一些有意义的事情,如果随随便便.浑浑噩噩的过日子,那就太可惜了 原文地址:https://www.cnblogs.com/onelikeone/p/9162382.html

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

上一篇博客我讲解了TagHelper的基本用法和自定义标签的生成,那么我就趁热打铁,和大家分享一下TagHelper的高级用法~~,大家也可以在我的博客下随意留言. 对于初步接触asp.net core的骚年可以看看我对TagHelper的了解和看法: <asp.net core新特性(1):TagHelper> 之后,我也会继续撰写博文,继续分享asp.net core的一些新特性,比如DI,ViewComponent以及bower等asp.net mvc中没有的新东西. ok,咱们就开始吧

asp.net同一次会话,SessionID总是变化问题解决

今天发现在一个Asp.net站点中,同一浏览器,同一次页面, 不停刷新页面, 此时后台Session的SessionID总是变化的. 经过调查发现,只需要手动加入Global.asax文件即可. 测试代码: using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace WebApplication1 { public partial class WebFor

分享NetSuite ERP免费测试后给服装企业带来的变化!

中国如今有大大小小的服装企业几十万家,行业竞争相当激烈,经济的全球化,给服装企业带来无限商机的同时也给他们的生产制造能力带来巨大的挑战.然而,许多服装企业在应对这些挑战的时候遇到不少问题,他们自己无法处理,从而极大地限制了企业的发展. 据统计,在我国服装企业中使用比较普遍的是财务软件和CAD软件,很少有能够将ERP系统高效地应用到整个企业中去的,绝大多数企业使用ERP系统的效果都不明显,能成功实施ERP系统的更是寥寥无几. 这其中的原因有以下几点: 1.系统行业性要求高.不同行业对ERP系统的需

ASP.NET Core 2.1 中 ViewResultExecutor 的变化

之前在 ASP.NET Core 2.0 中可以正常运行的代码: var services = HttpContext.RequestServices; var executor = services.GetRequiredService<ViewResultExecutor>(); var viewEngine = services.GetRequiredService<IRazorViewEngine>(); var view = viewEngine.GetView(null