(1-2)配置的升级 – ASP.NET从MVC5升级到MVC6
admin
2023-08-02 15:43:28
0

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

MVC5和MVC6的区别

在MVC5的时候,web.config文件无疑是整个配置的核心,从web配置,到应用程序的参数,都可以写在web.config里面的。
一个典型的web.config看上去是这样的。应用程序使用的各种设定,项目中的各种动态链接库及其版本信息,错误页面的指定,未知文件扩展名的注册等等,包罗万象。




  
    
    
    
    

    
    
    

    
    
    
    

    
    
    
    

    
    
    
    
    
    
    
    
    

    
    
    
    
    
    
    
    
  
  
  
    
    
    
  
  
    
      
        
        
      
      
        
        
      
      
        
        
      
      
        
        
      
      
        
        
      
      
        
        
      
      
        
        
      
    
  
  
    
      
      
      
      
    
    
      
      
      
      
      
      
    
  
  
    
      
    
  
  
    
      
      
    
  

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(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,使其可以运行和作为你的应用程式的宿主。




  

  
    
      
    
    
  

参考文献

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配置信息管理

相关内容

热门资讯

Mobi、epub格式电子书如... 在wps里全局设置里有一个文件关联,打开,勾选电子书文件选项就可以了。
定时清理删除C:\Progra... C:\Program Files (x86)下面很多scoped_dir开头的文件夹 写个批处理 定...
500 行 Python 代码... 语法分析器描述了一个句子的语法结构,用来帮助其他的应用进行推理。自然语言引入了很多意外的歧义,以我们...
scoped_dir32_70... 一台虚拟机C盘总是莫名奇妙的空间用完,导致很多软件没法再运行。经过仔细检查发现是C:\Program...
65536是2的几次方 计算2... 65536是2的16次方:65536=2⁶ 65536是256的2次方:65536=256 6553...
小程序支付时提示:appid和... [Q]小程序支付时提示:appid和mch_id不匹配 [A]小程序和微信支付没有进行关联,访问“小...
pycparser 是一个用... `pycparser` 是一个用 Python 编写的 C 语言解析器。它可以用来解析 C 代码并构...
微信小程序使用slider实现... 众所周知哈,微信小程序里面的音频播放是没有进度条的,但最近有个项目呢,客户要求音频要有进度条控制,所...
Apache Doris 2.... 亲爱的社区小伙伴们,我们很高兴地向大家宣布,Apache Doris 2.0.0 版本已于...
python清除字符串里非数字... 本文实例讲述了python清除字符串里非数字字符的方法。分享给大家供大家参考。具体如下: impor...