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.csappsettings.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差异变化的更多相关文章

  1. 简述关于ASP.NET MVC与.NET CORE 的区别

    简述关于ASP.NET MVC与.NET CORE的区别1.关于ASP.NET 关于MVC刚开始接触这个技术的时候我经常不理解他们的名字,我相信许多学ASP.NET开发人员开始接触MVC应该也和我一样 ...

  2. [ASP.NET MVC] ASP.NET Identity学习笔记 - 原始码下载、ID型别差异

    [ASP.NET MVC] ASP.NET Identity学习笔记 - 原始码下载.ID型别差异 原始码下载 ASP.NET Identity是微软所贡献的开源项目,用来提供ASP.NET的验证.授 ...

  3. 给外行或者刚入门普及一下关于C#,.NET Framework(.NET框架),.Net,CLR,ASP,ASP.Net, VS,以及.NET Core的概念

    一.概念 1. C# :C#是微软公司发布的一种面向对象的.运行于.NET Framework之上的高级程序设计语言. 2..NET Framework(.NET框架):.NET framework ...

  4. 比较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 ...

  5. WCF、WebAPI、WCFREST和Web服务的差异 ASP.NETMVC和ASP.NETWebAPI的差异

    WCF.WebAPI.WCFREST和Web服务的差异: Web服务 它是基于SOAP和XML的形式返回数据. 它仅支持HTTP协议. 它是开放源,但是不消耗任何客户端可以同时理解XML. 它可以仅在 ...

  6. ASP.NET与ASP.NET Core用户验证Cookie并存解决方案

    在你将现有的用户登录(Sign In)站点从ASP.NET迁移至ASP.NET Core时,你将面临这样一个问题——如何让ASP.NET与ASP.NET Core用户验证Cookie并存,让ASP.N ...

  7. 从Asp .net到Asp core (第二篇)《Asp Core 的生命周期》

    前面一篇文章简单回顾了Asp .net的生命周期,也简单提到了Asp .net与Asp Core 的区别,我们说Asp Core不在使用Asp.netRuntime,所以它也没有了web程序生命周期中 ...

  8. 从Asp .net到Asp core (第一篇)《回顾Asp .net生命周期与管道机制》

    从2016年微软收购了Xamarin整合到Visual Studio里并将其开源到现在已有三年多时间,从.net core 1.0 到现在的2.2,以及即将问世的3.0,我们看到微软正在跨平台之路越走 ...

  9. 一、Linux平台部署ASP.NET、ASP.NET CORE、PHP

    一.什么是Jexus Jexus是一款Linux平台上的高性能WEB服务器和负载均衡网关服务器,以支持ASP.NET.ASP.NET CORE.PHP为特色,同时具备反向代理.入侵检测等重要功能.可以 ...

随机推荐

  1. Prometheus学习

    简介 Prometheus 最初是 SoundCloud 构建的开源系统监控和报警工具,是一个独立的开源项目,于2016年加入了 CNCF 基金会,作为继 Kubernetes 之后的第二个托管项目. ...

  2. oracle 排序后分页查询

    demo: select * from ( select * from DEV_REG_CFG_CAMERA where 1 = 1 order by unid asc) where rownum & ...

  3. ValueError:GraphDef cannot be larger than 2GB.解决办法

    在使用TensorFlow 1.X版本的estimator的时候经常会碰到类似于ValueError:GraphDef cannot be larger than 2GB的报错信息,可能的原因是数据太 ...

  4. c# 调用 C++ dll 传入传出类型对应说明(转)

    由于经常使用C#调用 非托管C++ dll 操作一下硬件,出现传入传出类型的问题,现整理了C++ dll 类型与 C#类型对应关系: //C++中的DLL函数原型为        //extern & ...

  5. 我的洛谷签名——Pale Blue Dot

    We succeeded in taking that picture [from deep space], and, if you look at it, you see a dot. That's ...

  6. 常用 shell 命令 chmod | root

    chmod 命令 chmod 命令 [格式1:] chmod [ugoa][+-=][rwx] 文件或目录 /*u.g.o.a : u属主,g属组,o其他用户,a所有用户*/ /*+.-.= : 增加 ...

  7. MongoDB 聚合函数

    概念 聚合函数是对一组值执行计算并返回单一的值 主要的聚合函数 count distinct Group MapReduce 1.count db.users.count() db.users.cou ...

  8. 【luoguP4720】【模板】扩展卢卡斯

    快速阶乘与(扩展)卢卡斯定理 \(p\)为质数时 考虑 \(n!~mod~p\) 的性质 当\(n>>p\)时,不妨将\(n!\)中的因子\(p\)提出来 \(n!\) 可以写成 \(a* ...

  9. while循环 运算符和编码

    昨日回顾 1. 初识python python是一门弱类型的解释型高级编程语言 解释器: CPython 官方提供的默认解释器. c语言实现的 PyPy 把python程序一次性进行编译. IPyth ...

  10. WAMP完整配置教程(启用php extensions、修改端口、允许外网访问、wamp绑定域名)。

    作为一名php爱好者,很希望自己的写的代码能够快速的在浏览器页面展现出来,wamp是一款集成很完善.很方便的软件,我刚开始研究的时候,会因为对于wamp的不熟悉,导致修改一点点配置就会在百度查好久,这 ...