西雅图时间5月10日,微软在 Build 2017 大会上发布了 ASP.NET Core 2.0 Preview 1 ( 详见 Announcing ASP.NET 2.0.0-Preview1 and Updates for .NET Web Developers )。

以下是我关注的、并且经过自己实际验证的贴心的新特性:

1)Microsoft.AspNetCore.All —— 1包携10包,省力又省心

使用 ASP.NET Core 2.0 只需要安装一个 NuGet 组合包 —— Microsoft.AspNetCore.All,发布时会自动排除没有用到的包。

2)WebHost.CreateDefaultBuilder() —— 1行替10行,配置更简洁

ASP.NET Core 2.0 中的 Prgram.cs :

public class Program
{
public static void Main(string[] args)
{
BuildWebHost(args).Run();
} public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.Build();
}

上面的 1 行 WebHost.CreateDefaultBuilder(args) 取代了 ASP.NET Core 1.x 中的 10+ 行代码(Program.cs + Startup.cs)。

ASP.NET Core 1.x 中的 Program.cs :

public class Program
{
public static void Main(string[] args)
{
var host = new WebHostBuilder()
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseStartup<Startup>()
.Build(); host.Run();
}
}

ASP.NET Core 1.x 中的 Startup.cs:

public class Startup
{
public Startup(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
.AddEnvironmentVariables();
Configuration = builder.Build();
} public IConfigurationRoot Configuration { get; } public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
loggerFactory.AddDebug();
}
}

对应的 ASP.NET Core 2.0 中的 Startup.cs :

public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
} public IConfiguration Configuration { get; } public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
}
}

3)LoggerFactory.AddFilter(IDictionary<string, LogLevel> filter) 

ASP.NET Core 2.0 中通过代码配置日志过滤(Program.cs):

public class Program
{
public static void Main(string[] args)
{
BuildWebHost(args).Run();
} public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.ConfigureLogging(factory =>
{
factory.UseConfiguration(null);
factory.AddFilter(
new Dictionary<string, LogLevel>
{
{ "Micrsoft", LogLevel.Debug }
});
factory.AddConsole();
})
.UseStartup<Startup>()
.Build();
}

或者

public static class Program
{
public static void Main(string[] args)
{
var host = new WebHostBuilder()
.ConfigureLogging(factory =>
{
factory.AddConsole();
factory.AddFilter("Console", level => level >= LogLevel.Information);
})
//...
}
}

ASP.NET Core 1.x 中(Startup.cs):

loggerFactory.WithFilter(new FilterLoggerSettings
{
{ "Microsoft", LogLevel.Debug }
});

4)appsettings.json 中日志配置的变化

ASP.NET Core 2.0 中的日志配置如下,可直接在配置文件中根据 logging provider 配置对应的日志级别:

{
"Logging": {
"IncludeScopes": false,
"Debug": {
"LogLevel": {
"Default": "Warning"
}
},
"Console": {
"LogLevel": {
"Default": "Warning"
}
}
}
}

ASP.NET Core 1.x 中除了在配置文件中进行配置,还需要在代码中进行配置。

appsettings.json

{
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Warning"
}
}
}

Startup.cs

loggerFactory.AddConsole(Configuration.GetSection("Logging"));

5)Razor Pages

通过 Razor Pages ,只需 View,无需 Controller ,就可以直接访问 。

比如下面的Index.cshtml,直接就可以通过 http://localhost:5000/index 访问

ASP.NET Core 2.0 Preview 1 中贴心的新特性的更多相关文章

  1. 【译】.NET Core 3.0 Preview 3中关于ASP.NET Core的更新内容

      .NET Core 3.0 Preview 3已经推出,它包含了一系列关于ASP.NET Core的新的更新. 下面是该预览版的更新列表: Razor组件改进: 单项目模板 新的Razer扩展 E ...

  2. .NET Core 3.0 Preview 6中对ASP.NET Core和Blazor的更新

    我们都知道在6月12日的时候微软发布了.NET Core 3.0的第6个预览版.针对.NET Core 3.0的发布我们国内的微软MVP-汪宇杰还发布的官翻版的博文进行了详细的介绍.具体的可以关注&q ...

  3. asp.net core 1.1 项目升级至 asp.net core 2.0 preview 2 与正式版

    这两天把一个 asp.net core 1.1 的项目迁移到了 asp.net core 2.0 preview 2 ,在这篇随笔中记录一下. 如果项目在有 global.json 文件,需要删除或修 ...

  4. 从ASP.NET Core 3.0 preview 特性,了解CLR的Garbage Collection

    前言 在阅读这篇文章:Announcing Net Core 3 Preview3的时候,我看到了这样一个特性: Docker and cgroup memory Limits We conclude ...

  5. 探索ASP.Net Core 3.0系列四:在ASP.NET Core 3.0的应用中启动时运行异步任务

    前言:在本文中,我将介绍ASP.NET Core 3.0 WebHost的微小更改如何使使用IHostedService在应用程序启动时更轻松地运行异步任务. 翻译 :Andrew Lock   ht ...

  6. Asp.Net Core 2.0实现HttpResponse中繁切换

    随笔背景:因为项目中有个简单的功能是需要实现中文简体到繁体的切换,数据库中存储的源数据都是中文简体的,为了省事就想着通过HttpHeader的方式来控制Api返回对应的繁体数据. 实现方式:通过Asp ...

  7. 在ASP.NET Core 2.0 web项目中使用EntityFrameworkCore

    一.安装EFCode包 EFCore需要根据不同的数据库选择不同的数据库提供程序database provider,各数据库的包地址:https://docs.microsoft.com/zh-cn/ ...

  8. ASP.NET Core 3.0 自动挡换手动挡:在 Middleware 中执行 Controller Action

    最近由于发现奇怪的 System.Data.SqlClient 性能问题(详见之前的博文),被迫提前了向 .NET Core 3.0 的升级工作(3.0 Preview 5 中问题已被修复).郁闷的是 ...

  9. ASP.NET Core 3.0中使用动态控制器路由

    原文:Dynamic controller routing in ASP.NET Core 3.0 作者:Filip W 译文:https://www.cnblogs.com/lwqlun/p/114 ...

随机推荐

  1. 视觉SLAM中的数学基础 第三篇 李群与李代数

    视觉SLAM中的数学基础 第三篇 李群与李代数 前言 在SLAM中,除了表达3D旋转与位移之外,我们还要对它们进行估计,因为SLAM整个过程就是在不断地估计机器人的位姿与地图.为了做这件事,需要对变换 ...

  2. 基于CentOS搭建Nginx 静态网站

    系统要求: CentOS 7.2 64 位操作系统 一. 安装 Nginx(在 CentOS 上,可直接使用 yum 来安装 Nginx) yum install nginx -y 安装完成后,使用 ...

  3. 11G新特性 -- variable size extents

    AU是asm磁盘分配的基本单元.在oracle10g中,一个AU对应一个extent(这会增加对内存的使用),因为一个大的数据库如果含有大量的默认大小的AU,会导致数据库的share pool的大量使 ...

  4. Ubuntu 13.10 安装Terminalx 后更改默认终端设置

    1.安装 terminalx, sudo apt-get install terminator 2.Ctrl+ Alt + t 试一下打开什么终端,我的默认启动的是Terminator;如果想换换默认 ...

  5. 如何禁止VS显示“You have mixed tabs and spaces. Fix this?”

    如何禁止VS显示“You have mixed tabs and spaces. Fix this?” VS2013 版本的解决方案: Vs2013  IDE下,编辑C++的工程源码,在打开文件的时候 ...

  6. 数据源从druid迁移到HikariCP

    最近正好在做新项目,使用的是druid数据源,也真是巧,有朋友建议我使用HikariCP这个数据源,可以说是牛的一笔,速度快的飞起,性能极高! 要比druid不知道好多少倍,druid其实在某些情况下 ...

  7. Socket网络编程--聊天程序(9)

    这一节应该是聊天程序的最后一节了,现在回顾我们的聊天程序,看起来还有很多功能没有实现,但是不管怎么说,都还是不错的.这一节我们将讲多服务器问题(高大上的说法就是负载问题了.)至于聊天程序的文件发送(也 ...

  8. python prettytable模块

    简介 Python通过PrettyTable模块可以将输出内容如表格方式整齐地输出. 安装 pip install prettytable 1 示例 from prettytable import P ...

  9. select 语法

    select 语句主要语法: SELECT select_list [ INTO new_table ] FROM table_source [ WHERE search_condition ] [ ...

  10. linux每日命令(1):ls命令

    ls命令是linux下最常用的命令.ls命令就是list的缩写缺省下ls用来打印出当前目录的清单如果ls指定其他目录那么就会显示指定目录里的文件及文件夹清单. 通过ls 命令不仅可以查看linux ...