ASP.NET从MVC5升级到MVC6 总目录

MVC5和MVC6的区别

在MVC5的时候,web.config文件无疑是整个配置的核心,从web配置,到应用程序的参数,都可以写在web.config里面的。

一个典型的web.config看上去是这样的。应用程序使用的各种设定,项目中的各种动态链接库及其版本信息,错误页面的指定,未知文件扩展名的注册等等,包罗万象。

<?xml version="1.0" encoding="utf-8"?>
<!--
For more information on how to configure your ASP.NET application, please visit
http://go.microsoft.com/fwlink/?LinkId=301880
-->
<configuration>
<appSettings>
<add key="webpages:Version" value="3.0.0.0" />
<add key="webpages:Enabled" value="false" />
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" /> <add key="URLBASE" value="http://codesnippet.info" />
<add key="DEBUGMODE" value="true" />
<!--<add key="DEBUGMODE" value="false" />--> <!-- Image-->
<!--<add key="Image" value="Mongo" />-->
<add key="Image" value="FileSystem" />
<!--<add key="Image" value="QiNiu" />--> <!-- Thumbnail -->
<!--<add key="Thumbnail" value="Mongo" />-->
<add key="Thumbnail" value="FileSystem" />
<!--<add key="Thumbnail" value="QiNiu" />--> <!-- Github OAuth -->
<add key="GITHUB:ClientSecret" value="123456" />
<add key="GITHUB:ClientID" value="123456" />
<add key="GITHUB:AppName" value="codesnippet" />
<!-- QQ OAuth-->
<add key="QQ:AppID" value="123456" />
<add key="QQ:AppKey" value="123456"/>
<add key="QQ:CallBack" value="http://www.codesnippet.info/Home/QQOAuth"/>
<add key="QQ:AuthorizeURL" value="https://graph.qq.com/oauth2.0/authorize" /> <!-- QiNiu Storage -->
<add key="QINIU:AK" value="123456" />
<add key="QINIU:SK" value="123456" />
<add key="QINIU:BUCKET" value="gridfilesystem" />
<add key="QINIU:URLBASE" value="123456" />
<!-- Search Method -->
<!--<add key="SearchMethod" value="MongoTextSearch" />-->
<add key="SearchMethod" value="ElasticSearch" />
</appSettings>
<!--
For a description of web.config changes see http://go.microsoft.com/fwlink/?LinkId=235367. The following attributes can be set on the <httpRuntime> tag.
<system.Web>
<httpRuntime targetFramework="4.5.1" />
</system.Web>
-->
<system.web>
<compilation debug="true" targetFramework="4.5.1" />
<httpRuntime targetFramework="4.5.1" />
<customErrors mode="Off" />
</system.web>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Newtonsoft.Json" culture="neutral" publicKeyToken="30ad4fe6b2a6aeed" />
<bindingRedirect oldVersion="0.0.0.0-8.0.0.0" newVersion="8.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.Optimization" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="1.1.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="0.0.0.0-1.6.5135.21930" newVersion="1.6.5135.21930" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-5.2.3.0" newVersion="5.2.3.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Antlr3.Runtime" publicKeyToken="eb42632606e9261f" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-3.5.0.2" newVersion="3.5.0.2" />
</dependentAssembly>
</assemblyBinding>
</runtime>
<system.webServer>
<handlers>
<remove name="ExtensionlessUrlHandler-Integrated-4.0" />
<remove name="OPTIONSVerbHandler" />
<remove name="TRACEVerbHandler" />
<add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
</handlers>
<staticContent>
<remove fileExtension=".woff"/>
<remove fileExtension=".woff2"/>
<remove fileExtension=".svg"/>
<mimeMap fileExtension=".woff" mimeType="application/x-font-woff" />
<mimeMap fileExtension=".woff2" mimeType="application/x-font-woff" />
<mimeMap fileExtension=".svg" mimeType="image/svg+xmz" />
</staticContent>
</system.webServer>
<system.net>
<settings>
<httpWebRequest useUnsafeHeaderParsing="false" />
</settings>
</system.net>
<system.codedom>
<compilers>
<compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:6 /nowarn:1659;1699;1701" />
<compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:14 /nowarn:41008 /define:_MYTYPE=\&quot;Web\&quot; /optionInfer+" />
</compilers>
</system.codedom>
</configuration>

MVC6的配置

首先,MVC的默认配置变成了JSON文件。(ASP.NET Core的配置管理支持XML,JSON,INI三种方式。)

你可以将配置文件做成自己喜欢的文件格式,或者多个文件格式混用。

(以下示例代码来自 XiaoFaye的Github开源示例,三个配置文件的内容这里省略了,详细请参见Github)

using System;
using Microsoft.Extensions.Configuration; namespace ConsoleApplication
{
public class Program
{
public static void Main(string[] args)
{
var builder = new ConfigurationBuilder()
.AddJsonFile("appsettings.json")
.AddXmlFile("appsettings.xml")
.AddIniFile("appsettings.ini"); builder.AddEnvironmentVariables();
IConfigurationRoot Configuration = builder.Build(); Console.WriteLine("Read json configuration file: " + Configuration["JsonData:DefaultConnection:ConnectionString"]);
Console.WriteLine(".");
Console.WriteLine(".");
Console.WriteLine("Read xml configuration file: ");
Console.WriteLine("Xml node content: " + Configuration["DefaultConnection"]);
Console.WriteLine("Xml node attribute 1: " + Configuration["DefaultConnection:key"]);
Console.WriteLine("Xml node attribute 2: " + Configuration["DefaultConnection:value"]);
Console.WriteLine(".");
Console.WriteLine(".");
Console.WriteLine("Read ini configuration file: " + Configuration["IniData:DefaultConnection"]);
}
}
}

典型的Project.Json

Converting an ASP.NET Core RC1 Project to RC2

配置文件比较长,这里就不再完整列出了,如果你想查看一个完整的例子。请使用以下链接

一个完整的Project.json的例子

这里有一个注意点,ASP.NET Core可以同时支持经典的NET Framework 和 NET Core,使用哪个Framework,可以在配置里面定义。

  • 如果你想使用全新的.NET Core的Framework ,请这样配置你的 framework 节
"frameworks": {
"netcoreapp1.0": {
"imports": [
"dotnet5.6",
"dnxcore50",
"portable-net45+win8"
]
}
},
  • 如果您想使用.NET Framework 4.5.x,请这样配置你的 framework 节
"frameworks": {
"net452": { }
},

如果您想知道两个Framework之间的区别您可以阅读本站以下文章:

补充知识 .Net Framework .Net Core Mono 区别联系

一个简单的配置文件

{
"version": "1.0.0-*",
"buildOptions": {
"preserveCompilationContext": true,
"emitEntryPoint": true
},
"dependencies": {
"Microsoft.AspNetCore.Mvc": "1.0.0-*",
"Microsoft.AspNetCore.Mvc.TagHelpers": "1.0.0-*",
"Microsoft.AspNetCore.Diagnostics": "1.0.0-*",
"Microsoft.AspNetCore.StaticFiles": "1.0.0-*",
"Microsoft.AspNetCore.Server.IISIntegration": "1.0.0-*",
"Microsoft.AspNetCore.Server.Kestrel": "1.0.0-*",
"Microsoft.Extensions.Logging.Console": "1.0.0-*",
"Microsoft.Extensions.Logging.Debug": "1.0.0-*",
"Microsoft.NETCore.App": {
"type": "platform",
"version": "1.0.0-rc2-3002700"
}
},
"frameworks": {
"netcoreapp1.0": {
"imports": "dnxcore50"
}
}
}

典型的appsettings.json

有一些关于应用程序业务逻辑,环境配置的参数,原来也是写在Web.config里面的,现在这个版本可以放到appsettings.json

我们看一下官方自动生成的appsettings.json是什么样子的。

数据库的连接字符串,日志设定,这些被放到了appsettings.json里面了。

{
"ConnectionStrings": {
"DefaultConnection": "Server=(localdb)\\mssqllocaldb;Database=aspnet-NetCoreWeb-dff472d2-ddfc-4d7a-bb9c-19ab91e3564f;Trusted_Connection=True;MultipleActiveResultSets=true"
},
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Debug",
"System": "Information",
"Microsoft": "Information"
}
}
}

注意:和project.json这个第一等公民不同,appsettings.json属于第二等公民(可选非必需)

它需要在启动的时候,引入Configuration系统。当然,笔者认为,即使你修改名称也是可以的,只要代码里面也相应修改即可。

            var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true);

使用方法是这样的:

            loggerFactory.AddConsole(Configuration.GetSection("Logging"));
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));

当然,如果你仔细研究的话,web.config文件还是有的。应该是在IIS模式的时候,IIS是必须有web.config的(可能不对,请指正)。

这里增加了AspNetCoreModule这个Handler。

Announcing ASP.NET Core RC2

IIS Support Baked In

IIS is a tremendous web server and we continue to provide first-class support for it. Use the new ASP.NET Core Module in your web.config (it’s added in the new project templates) to configure IIS to launch and host your application.

IIS是一个极其重要的Web服务器,我们继续为其提供最高等级的支持。使用新的ASP.NET Core Module在你的Web.config中,用来配置你的IIS,使其可以运行和作为你的应用程式的宿主。

<?xml version="1.0" encoding="utf-8"?>
<configuration> <!--
Configure your application settings in appsettings.json. Learn more at http://go.microsoft.com/fwlink/?LinkId=786380
--> <system.webServer>
<handlers>
<add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified"/>
</handlers>
<aspNetCore processPath="%LAUNCHER_PATH%" arguments="%LAUNCHER_ARGS%" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" forwardWindowsAuthToken="false"/>
</system.webServer>
</configuration>

参考文献

Converting an ASP.NET Core RC1 Project to RC2

ASP.NET官网Configuration

XiaoFaye的XiaoFaye/netcore-samples Github

Artech 博客园系列文章 .NET Core全新的配置管理[共9篇]

解读ASP.NET 5 & MVC6系列(5):Configuration配置信息管理

(1-2)配置的升级 - ASP.NET从MVC5升级到MVC6的更多相关文章

  1. ASP.NET从MVC5升级到MVC6 RC2 总目录 - 发布在RC2Release之后

    序言 随着MVC6RC2的推出,MVC6的脚步越来越近了.但是在我们的手里,有大批量的MVC5的项目.如何将MVC5升级到MVC6,将是一个很大的课题.微软官方暂时没有一个升级指导,或者一个迁移工具, ...

  2. (1-1)文件结构的升级(Area和Filter知识总结) - ASP.NET从MVC5升级到MVC6

    ASP.NET从MVC5升级到MVC6 总目录 MVC5项目结构 带有Areas和Filter的项目结构 一般来说,小的MVC项目是不考虑领域的,但是,如果是稍微复杂一点的项目,往往是需要领域这个概念 ...

  3. ASP.NET从MVC5升级到MVC6

    1-1)文件结构的升级(Area和Filter知识总结) - ASP.NET从MVC5升级到MVC6   ASP.NET从MVC5升级到MVC6 总目录 MVC5项目结构 带有Areas和Filter ...

  4. (0)ASP.NET Core 简单介绍 和开发环境搭建 - ASP.NET从MVC5升级到MVC6

    ASP.NET从MVC5升级到MVC6 总目录 ASP.NET Core 首先,ASP原本只是一种技术,这种技术从上个世纪90年代就有了. Active Service Page 和 Java Ser ...

  5. visual studio 2012打开提示 未能将网站×××配置为使用 ASP.NET 4.5 和 尚未在Web服务器上注册,您需要手动将Web服务器配置为使用ASP.NET 4.5

    未能将网站×××配置为使用 ASP.NET 4.5.为了使此网站正确运行,您必须将它手动配置为使用ASP.NET 4.5. ASP.NET 4.5尚未在Web服务器上注册,您需要手动将Web服务器配置 ...

  6. CentOS 升级 Python3 (附带: 一键升级脚本)

      升级环境   应用名称 版本 Python 3.5.2 Syatem CentOS 6.7         升级方法   [1]下载 Python 3: wget http://mirrors.s ...

  7. 实现乐鑫esp8266的无线OTA升级,实现远程在线升级固件

    代码地址如下:http://www.demodashi.com/demo/12994.html 一.前言: 写了这么多的8266博文,一直以满意100%的心态去敲写代码固件烧录,以致很少出现 bug ...

  8. ESA2GJK1DH1K升级篇: 快速的移植升级程序到自己的项目(APP用户程序制作)

    前言 用户程序比较简单,但是起着至关重要的作用 用户程序是和BootLoader程序相互配合的 拷贝文件到自己的项目 APP用户程序的 stmflash.c stmflash.h 和 上一节的Boot ...

  9. Veritas NetBackup 8.1.2 升级的主要理由--附升级兼容性检查网址

    管理更简单 NetBackup™ 8.1.2 基于 Web 的全新直观用户界面让操作变得极度简单化,最常用操作现在只需单击几次或触摸几下即可完 成.通过台式机或移动设备可为不同角色的其他用户授予访问权 ...

随机推荐

  1. NodeJs 开发微信公众号(二)测试环境部署

    由于卤煮本人是做前端开发的,所以在做公众号过程中基本上没有遇到前端问题,在这方面花的时间是最少的.加上用了mui框架(纯css界面)和自己积累的代码,很快地开发出了界面来.接着是后台开发.卤煮选的是n ...

  2. Opengl中矩阵和perspective/ortho的相互转换

    Opengl中矩阵和perspective/ortho的相互转换 定义矩阵 Opengl变换需要用四维矩阵.我们来定义这样的矩阵. +BIT祝威+悄悄在此留下版了个权的信息说: 四维向量 首先,我们定 ...

  3. 同一AppDomain内的SharpGL的FrameBuffer会相互影响?

    同一AppDomain内的SharpGL的FrameBuffer会相互影响? 今天做一个试验时,偶然发现了这样一个问题. 以SharpGL的示例项目SimpleDrawingSample为例. 给Fo ...

  4. [ASP.NET MVC 小牛之路]05 - 使用 Ninject

    在[ASP.NET MVC 小牛之路]系列上一篇文章(依赖注入(DI)和Ninject)的末尾提到了在ASP.NET MVC中使用Ninject要做的两件事情,续这篇文章之后,本文将用一个实际的示例来 ...

  5. 如何在 ASP.NET MVC 中集成 AngularJS(2)

    在如何在 ASP.NET MVC 中集成 AngularJS(1)中,我们介绍了 ASP.NET MVC 捆绑和压缩.应用程序版本自动刷新和工程构建等内容. 下面介绍如何在 ASP.NET MVC 中 ...

  6. Oracle 中 union 和union all 的简单使用说明

    1.刚刚工作不久,经常接触oracle,但是对oracle很多东西都不是很熟.今天我们来了解一下union和union all的简单使用说明.Union(union all): 指令的目的是将两个 S ...

  7. angularjs 2.0 快速案例(1)

    前言 上一节我们已经把环境给搭建起来了,现在我们通过一个快速案例把angular 2.0 初步了解一下,后续我们会深入每一个细节,这个案例主要是一个[英雄(Hero)]列表的展示,创建,编辑.这个案例 ...

  8. eclipse中的javac命令与java命令

    一.eclipse的javac命令:当eclipse对.java(源文件)文件进行保存操作时(快捷键ctrl+s),会执行javac命令.见上图,Default output folder(默认输出文 ...

  9. Sublime Text执行js

    Sublime Text执行js 在Build Sytem添加以下内容: { "cmd": ["node", "$file"], " ...

  10. easyuidatagrid中load,reload,loadData的区别

    摘要:datagrid中有load,reload,loadData那三个方式,皆是加载数据的,但又有差别.下面让我们一起来看看: 首先,load方法,比如我已经定义一个datagrid的id为grid ...