从ASP.NET到ASP.NET Core差异变化
MSDN原文:链接
ASP.NET Core项目为开发人员提供了针对.NET Core,.NET Framework2种实现方式,根据官网通告NETCORE3.0后将取消对.NET Framework的支持。
NET Framework下使用
在针对.NET Framework时,项目需要引用NuGet AspNetCore包。
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.App" />
</ItemGroup>
项目结构差异:
.csproj的文件在ASP.NET Core下进行了简化:
明确包含文件不是他们被视为项目的一部分。这可以降低在处理大型团队时出现XML合并冲突的风险。
没有其他项目的基于GUID的引用,这提高了文件的可读性。
可以在不在Visual Studio中卸载文件的情况下编辑该文件
程序入口和Global.asax文件变更:
ASP.NET Core引入了新的引导应用程序机制,ASP.NET 中入口点是Global.asax文件。路径配置和过滤器以及区域注册等任务在Global.asax文件中处理。
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
}
}
此方法以干扰实现的方式将应用程序与其部署的服务器耦合在一起。为了解耦,OWIN被引入以提供一种更清晰的方式来一起使用多个框架。OWIN提供了一个管道,只添加所需的模块。托管环境采用Startup功能来配置服务和应用程序的请求管道。
使用ASP.NET Core 后,应用程序的入口点是Startup
,不再依赖Global.asax。
ASP.NET CORE +NET Framework 实现(这会配置您的默认路由,默认为Json上的XmlSerialization。根据需要将其他中间件添加到此管道(加载服务,配置设置,静态文件等):
using Owin;
using System.Web.Http; namespace WebApi
{
// Note: By default all requests go through this OWIN pipeline. Alternatively you can turn this off by adding an appSetting owin:AutomaticAppStartup with value “false”.
// With this turned off you can still have OWIN apps listening on specific routes by adding routes in global.asax file using MapOwinPath or MapOwinRoute extensions on RouteTable.Routes
public class Startup
{
// Invoked once at startup to configure your application.
public void Configuration(IAppBuilder builder)
{
HttpConfiguration config = new HttpConfiguration();
config.Routes.MapHttpRoute("Default", "{controller}/{customerID}", new { controller = "Customer", customerID = RouteParameter.Optional }); config.Formatters.XmlFormatter.UseXmlSerializer = true;
config.Formatters.Remove(config.Formatters.JsonFormatter);
// config.Formatters.JsonFormatter.UseDataContractJsonSerializer = true; builder.UseWebApi(config);
}
}
}
ASP.NET CORE+Net Core实现中使用类似的方法,但不依赖于OWIN,相反,通过Program.cs Main
方法(类似于控制台应用程序)完成的,并通过Startup
加载。
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting; namespace WebApplication2
{
public class Program
{
public static void Main(string[] args)
{
CreateWebHostBuilder(args).Build().Run();
} public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>();
}
}
Startup
必须包括一种Configure
方法。在Configure
,向管道添加必要的中间件。在以下示例中(来自默认网站模板),扩展方法配置管道,并支持:
- 错误页面
- HTTP严格传输安全性
- HTTP重定向到HTTPS
- ASP.NET核心MVC
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseHsts();
} app.UseHttpsRedirection();
app.UseMvc();
}
Appliction和Host已经分离,这为迁移到不同平台提供了灵活性。
配置文件差异:
ASP.NET支持存储配置。例如,这些设置用于支持部署应用程序的环境。通常的做法是将所有自定义键值对存储<appSettings>
在Web.config文件的部分中:
<appSettings>
<add key="UserName" value="User" />
<add key="Password" value="Password" />
</appSettings>
ASP.NET 应用程序使用命名空间中的ConfigurationManager.AppSettings
集合读取这些设置System.Configuration
:
string userName = System.Web.Configuration.ConfigurationManager.AppSettings["UserName"];
string password = System.Web.Configuration.ConfigurationManager.AppSettings["Password"];
ASP.NET Core可以将应用程序的配置数据存储在任何文件中,并将其作为中间件引导的一部分加载。项目模板中使用的默认文件是appsettings.json:
{
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Debug",
"System": "Information",
"Microsoft": "Information"
}
},
// Here is where you can supply custom configuration settings, Since it is is JSON, everything is represented as key: value pairs
// Name of section is your choice
"AppConfiguration": {
"UserName": "UserName",
"Password": "Password"
}
}
通过Startup.cs将appsettings.json文件加载到IConfiguration
应用程序内部的实例中 :
public Startup(IConfiguration configuration)
{
Configuration = configuration;
} public IConfiguration Configuration { get; }
读取Configuration
以获取设置
string userName = Configuration.GetSection("AppConfiguration")["UserName"];
string password = Configuration.GetSection("AppConfiguration")["Password"];
对于更深入的参考ASP.NET Core配置,请查看 在ASP.NET核心配置。
从ASP.NET到ASP.NET Core差异变化的更多相关文章
- 简述关于ASP.NET MVC与.NET CORE 的区别
简述关于ASP.NET MVC与.NET CORE的区别1.关于ASP.NET 关于MVC刚开始接触这个技术的时候我经常不理解他们的名字,我相信许多学ASP.NET开发人员开始接触MVC应该也和我一样 ...
- [ASP.NET MVC] ASP.NET Identity学习笔记 - 原始码下载、ID型别差异
[ASP.NET MVC] ASP.NET Identity学习笔记 - 原始码下载.ID型别差异 原始码下载 ASP.NET Identity是微软所贡献的开源项目,用来提供ASP.NET的验证.授 ...
- 给外行或者刚入门普及一下关于C#,.NET Framework(.NET框架),.Net,CLR,ASP,ASP.Net, VS,以及.NET Core的概念
一.概念 1. C# :C#是微软公司发布的一种面向对象的.运行于.NET Framework之上的高级程序设计语言. 2..NET Framework(.NET框架):.NET framework ...
- 比较ASP.NET和ASP.NET Core[经典 Asp.Net v和 Asp.Net Core (Asp.Net Core MVC)]
ASP.NET Core是.与.Net Core FrameWork一起发布的ASP.NET 新版本,最初被称为ASP.NET vNext,有一系列的命名变化,ASP.NET 5.0,ASP.NET ...
- WCF、WebAPI、WCFREST和Web服务的差异 ASP.NETMVC和ASP.NETWebAPI的差异
WCF.WebAPI.WCFREST和Web服务的差异: Web服务 它是基于SOAP和XML的形式返回数据. 它仅支持HTTP协议. 它是开放源,但是不消耗任何客户端可以同时理解XML. 它可以仅在 ...
- ASP.NET与ASP.NET Core用户验证Cookie并存解决方案
在你将现有的用户登录(Sign In)站点从ASP.NET迁移至ASP.NET Core时,你将面临这样一个问题——如何让ASP.NET与ASP.NET Core用户验证Cookie并存,让ASP.N ...
- 从Asp .net到Asp core (第二篇)《Asp Core 的生命周期》
前面一篇文章简单回顾了Asp .net的生命周期,也简单提到了Asp .net与Asp Core 的区别,我们说Asp Core不在使用Asp.netRuntime,所以它也没有了web程序生命周期中 ...
- 从Asp .net到Asp core (第一篇)《回顾Asp .net生命周期与管道机制》
从2016年微软收购了Xamarin整合到Visual Studio里并将其开源到现在已有三年多时间,从.net core 1.0 到现在的2.2,以及即将问世的3.0,我们看到微软正在跨平台之路越走 ...
- 一、Linux平台部署ASP.NET、ASP.NET CORE、PHP
一.什么是Jexus Jexus是一款Linux平台上的高性能WEB服务器和负载均衡网关服务器,以支持ASP.NET.ASP.NET CORE.PHP为特色,同时具备反向代理.入侵检测等重要功能.可以 ...
随机推荐
- Jmeter jmeter-server.bat 无法启动
问题现象: 解决方法: 找到如下文件: 在目录\apache-jmeter-5.0\bin下,打开名为jmeter.properties的文件 找到server.rmi.ssl.disable=fal ...
- mysql字符串截取函数和日期函数
注:mysql下标索引从1开始,并包含开始索引 1.left(str,len) index<=0,返回空 index>0,截取最左边len个字符 select ), ), ), ) 结果 ...
- python 监听键盘输入
import sys, select, tty, termios old_attr = termios.tcgetattr(sys.stdin) tty.setcbreak(sys.stdin.fil ...
- seaborn---画热力图
1.引用形式: seaborn.heatmap(data, vmin=None, vmax=None, cmap=None, center=None, robust=False, annot=None ...
- oVirt-postgresql
连接数据库 方法一: cd /opt/rh/rh-postgresql95/root/bin su postgres ./psql \c engine 执行sql语句即可 方法二: 用pgAdmin访 ...
- 记录一次群答问:jmeter正则提取器轻松提取一个及多个值
图截得比较完整,电脑端浏览器放大倍数看吧^_^,手机端可以点击图片然后放大看. 一个正则提取问题 前几天,在Q群和微信群里被同时@,咨询这样一个问题:服务器返回:name="tom" ...
- Maven 学习(一)-Maven 使用入门
http://www.cnblogs.com/xdp-gacl/p/3498271.html http://www.cnblogs.com/xdp-gacl/p/4240930.html 一.Mave ...
- iOS GPU、cpu、显示器的协作
在 iOS 系统中,图像内容展示到屏幕的过程需要 CPU 和 GPU 共同参与. CPU 负责计算显示内容,比如视图的创建.布局计算.图片解码.文本绘制等. 随后 CPU 会将计算好的内容提交到 GP ...
- Internal.Cryptography.CryptoThrowHelper+WindowsCryptographicException 拒绝访问 / 出现了内部错误 c# – 当使用X509Certificate2加载p12/pfx文件时出现
环境:iis/netcore 2.2 初始调用:X509Certificate2 certificate = new X509Certificate2(input.Path, CER_PASSWORD ...
- 入门平衡树: Treap
入门平衡树:\(treap\) 前言: 如有任何错误和其他问题,请联系我 微信/QQ同号:615863087 前置知识: 二叉树基础知识,即简单的图论知识. 初识\(BST\): \(BST\)是\( ...