西雅图时间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. Cache Line 伪共享发现与优化

    https://yq.aliyun.com/articles/465504 Cache Line 伪共享发现与优化 作者:吴一昊,杨勇 1. 关于本文 本文基于 Joe Mario 的一篇博客 改编而 ...

  2. OpenLayers典型部分概述

    中文学习:http://www.openlayers.cn/portal.php 原文地址:https://www.jianshu.com/p/e693711a7008 一 OpenLayers核心职 ...

  3. DropDMG for Mac(dmg 文件打包工具)破解版安装

    1.软件简介    DropDMG 是 macOS 系统上的一款帮助用户快速打包 DMG 文件的 Mac 文件管理软件,DropDMG 不但可以将影像档加密.更可以配合 GZip .BZip2 .Ma ...

  4. nginx与apache的参考配置

    nginx与apache是两大最主流的服务器,功能强大,但配置起来也比较麻烦,对于初学者来讲可能有些地方并不完全清楚其作用,这里搜集了一些配置的作用及其使用方法.其中nginx提供了推荐配置,而apa ...

  5. fiddler4 使用教程

    Fiddler是最强大最好用的Web调试工具之一,它能记录所有客户端和服务器的http和https请求,允许你监视,设置断点,甚至修改输入输出数据,Fiddler包含了一个强大的基于事件脚本的子系统, ...

  6. 使用tar解压文件提示gzip: stdin: not in gzip format错误

    使用tar解压文件提示gzip: stdin: not in gzip format错误 1. 问题描述 使用docker save xxxx > xxx.tar导出镜像,由于文件太大,需要sp ...

  7. YouTube上最火的十个大数据视频

    http://blog.jobbole.com/84148/ YouTube上最火的十个大数据视频

  8. maven 打jar 被引用后 出现 cannot resolve symbol 错误 生成jar包形式代码文件组织格式 非springboot文件组织格式

    项目A引用项目B A项目中pom引入没有报错,但是:1,idea里面查找到b项目中的代码时,会提示b代码中的引用不正确.提示无法解析语法 解压B的jar,发现目录是: springboot文件组织格式 ...

  9. Java知多少(1) 语言概述

    Java语言是SUN(Stanford University Network,斯坦福大学网络公司)公司1995年推出的一门高级编程语言,起初主要应用在小型消费电子产品上,后来随着互联网的兴起,Java ...

  10. Python之获取微信好友信息

    save_info.py: #!/usr/bin/python # -*- coding: UTF-8 -*- import itchat import pickle itchat.auto_logi ...