ASP.NET 5 Beta5来了(翻译)

时间:2023-03-09 15:37:00
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 5运行时的更新。

ASP.NET 5 可以运行在全功能的.NET库和核心库。.NET核心库能够运行在Windows,Azure,Linux和Mac上。是时候开始安装beta5并应用到你的ASP.NET 5项目中去。

下面是Beta 5的一些亮点:

.NET执行环境(DNX)

  • 支持Nuget v3。
    使用Nuget v3的路径可以更快地下载packages。尝试添加https://api.nuget.org/v3/index.json作为package源。
  • 支持全新的.NET目标框架监测器(TFM)。
  • 在project.json中可以设置语言和发布说明的链接
  • 消除了对JSON.NET的固定版本要求:即不再要求你的应用程序使用和DNX一致的JSON.NET版本,你可以选择新发布的JSON.NET而不需要升级DNX的版本号。
  • 新的IRuntimeEnvironment服务。使用新的Service可以获取到运行时具体信息,像OS,CLR等。

ASP.NET 5

  • HttpContext.Connection
    通过HttpContext的新加Connection属性可以获得连接的信息。
  • 新增本地化的抽象和中间件
    你可以在本地化的Sample里发现这些东西的使用。
  • 统一终止ASP.NET宿主环境的快捷键为Ctrl+C
    之前按任意键即可终止ASP.NET宿主环境的,现在统一终止快捷键为Ctrl+C。
MVC 6
  • 在Razor支持C# 6
    你可以通过MSDN上C# 6 了解C# 6的新特性。
  • 简化了MVC的选项设置,添加了*配置
    现在有了应用层的设置,可以用来为HTML helpers配置各种不同的设置。
  • 在视图中可以使用JSON Helper来序列化数据模型
    该功能可以让你在Razor view中非常容易地序列化你的.NET对象成JSON格式:
    @Json.Serialize(Model)
  • 在Route标记中能够使用通配替换
    [Route("Products/[action]", Name = "[actions]Products")]
    public class ProductsController
    {
    public void Add() { }
    public void Buy() { }
    }
  • 新的ImageTagHelper
    <img asp-file-version="true" src="~/images/my_cool_image.png" />  
  • Tag Helper支持绑定字典属性
    现在你可以在TagHelpers中绑定服务器端的attributes到字典属性。比如,AnchorTagHelper利用名字格式为asp-route-*的attributes来设置路由值。
    <a asp-action="Edit" asp-route-id="@index">Edit</a>
  • 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。比如

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

    就不会被应用上AnchorTagHelper。

大家可以在这里看到比较关于Beta5的详细的信息以及关于Beta5已知的问题。后续也会有更多的beta版本发布知道最终正式发布为止。

Beta5和Visual Studio 2015 RC是兼容的,你可以利用Visual Studio 2015 RC来打开、编译和运行基于Beta5运行库的ASP.NET 5应用。

在Visual Studio 2015RC上升级到Beta5需要做一下几步:

  • 如果之前没有安装,请安装 .NET Version Manager (DNVM)。如果已经安装了Visual Studio,请忽略这一步,直接进入下一步。
  • 设置系统环境变量DNX_FEED值为https://www.nuget.org/api/v2
  • 执行“dnvm upgrade”,升级dnx的版本号到Beta5
  • 更新应用中的global.json文件的sdk版本号到"1.0.0-beta5"(DNX)
  • 更新应用的依赖库的版本号到beta5
  • Restore新版本的依赖库
  • 编译应用并根据beta5的需要做相应的migration.

在beta5还有一些break changes,更详细的信息请参考Beta5 Release Note,希望大家能够喜欢Beta5。