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

  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. 虚拟机-VMware小结

    1.网卡的3种模式 桥接模式:虚拟机=物理机器,连接物理网卡,虚拟ip设置物理网卡的网段和网管.可上网. NAT模式:虚拟机把物理机器当做路由器,虚拟ip网段ip自动获取.可上网. https://w ...

  2. layui扩展组件sliderVerify 实现滑块验证

    首先在要使用的静态文件代码中引入‘./sliderVerify/sliderVerify.js‘ 先看看效果 示例代码 <!DOCTYPE html> <html> <h ...

  3. ML-软间隔(slack)的 SVM

    Why Slack? 为了处理异常值(outlier). 前面推导的svm形式, 是要求严格地全部分对, 基于该情况下, 在margin 的边界线 线上的点, 只能是支持向量. \(min_w \ \ ...

  4. rest framework 之渲染器

    根据 用户请求URL 或 用户可接受的类型,筛选出合适的 渲染组件. 用户请求头: Accept:text/html,application/xhtml+xml,application/xml;q=0 ...

  5. php解释器模式( interpreter pattern)

    ... <?php /* The interpreter pattern specifies how to evaluate language grammar or expressions. W ...

  6. dart 使用

    用法 说明 print('xxx') 打印 == 比较相等 != 比较不等 语句后面必须加分号

  7. 26、pathlib文件系统模块(了解)

    一.pathlib库官方定义 pathlib 是Python内置库,Python 文档给它的定义是 Object-oriented filesystem paths(面向对象的文件系统路径).path ...

  8. 记一次部署时报java.lang.NoSuchMethodError:javax.persistence.spi.PersistenceUnitInfo.getValidationMode()Ljavax / persistence / ValidationMode;的解决办法

    楼主在部署war包的时候,本地启动不报错,服务器商报如下问题: Error creating bean with name 'entityManagerFactory' defined in clas ...

  9. PHP获取一年有多少周和每周开始和结束日期

    /*PHP获取当前日期是第几周和本周开始日期和本周结束日期*/ //$now = '2018-11-13';周二 public function getNowTimeInfo($now) { $str ...

  10. LeetCode 722. Remove Comments

    原题链接在这里:https://leetcode.com/problems/remove-comments/ 题目: Given a C++ program, remove comments from ...