ASP.NET Core - 配置

在本章中,我们将讨论与ASP.NET Core项目相关的配置.在解决方案资源管理器中,您将看到Startup.cs文件.如果您使用过以前版本的ASP.NET Core,您可能希望看到一个global.asax文件,在这里您可以编写代码以在Web应用程序启动期间执行.

  • 您还希望看到一个web.config文件,其中包含应用程序执行所需的所有配置参数.

  • 在ASP.NET Core中,这些文件全部消失,而不是从Startup.cs加载配置和启动代码.

  • 文件中有一个Startup类,在这个类中,您可以配置应用程序甚至配置配置源.

这里是 Startup.cs文件中的默认实现.

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Threading.Tasks; 

using Microsoft.AspNetCore.Builder; 
using Microsoft.AspNetCore.Hosting; 
using Microsoft.AspNetCore.Http; 

using Microsoft.Extensions.DependencyInjection; 
using Microsoft.Extensions.Logging;  

namespace FirstAppDemo { 
   public class Startup { 
      // This method gets called by the runtime.
      // Use this method to add services to the container. 
      // For more information on how to configure your application, 
      // visit http://go.microsoft.com/fwlink/?LinkID=398940 
      public void ConfigureServices(IServiceCollection services) { 
      }  
      
      // This method gets called by the runtime. Use this method to configure 
      // the HTTP request pipeline.
      public void Configure(IApplicationBuilder app, IHostingEnvironment env, 
         ILoggerFactory loggerFactory) { 
         loggerFactory.AddConsole();  
         
         if (env.IsDevelopment()) { 
            app.UseDeveloperExceptionPage(); 
         }  
         app.Run(async (context) => { 
            await context.Response.WriteAsync("Hello World!"); 
         }); 
      } 
   } 
}

在Startup课程中,有两种方法可以让我们的大部分工作进行.该类的Configure方法是构建HTTP处理管道的地方.

  • 这定义了应用程序如何响应请求.目前这个应用程序只能说Hello World!如果我们希望应用程序的行为不同,我们需要通过在此Configure方法中添加其他代码来更改管道.

  • 例如,如果我们想要提供诸如index.html文件之类的静态文件,我们需要在Configure方法中添加一些代码.

  • 你也可能有错误页面或路由请求到ASP.NET MVC控制器;这两种情况都需要在此Configure方法中完成一些工作.

  • 在Startup类中,您还将看到 ConfigureServices() 方法.这有助于您为应用程序配置组件.

现在,我们为每个响应都有一个硬编码字符串 -  你好世界!串.我们希望从知道我们想要显示的文本的某个组件加载此字符串,而不是对字符串进行硬编码.

  • 这个其他组件可能会从数据库或Web服务或JSON文件加载该文本,无论它究竟在哪里都无关紧要.

  • 我们将设置一个场景,以便我们没有这个硬编码的字符串.

在解决方案资源管理器中,右键单击在项目节点上,选择添加 新项目.

添加第一项

在左侧窗格中,选择 Installed→代码然后在中间窗格中,选择JSON文件.调用此文件 AppSettings.json ,然后单击添加按钮,如上面的屏幕截图所示.

已安装代码

我们也可以让我们的程序从文件中读取文本,而不是拥有Hello World! Startup.cs中的字符串.让我们在 AppSettings.json文件中添加以下代码.

{ 
   "message": "Hello, World! this message is from configuration file..." 
}

现在我们需要从Startup.cs文件中访问此消息.以下是 Startup.cs 文件的实现,该文件将从JSON文件中读取上述消息.

using Microsoft.AspNet.Builder; 
using Microsoft.AspNet.Hosting; 
using Microsoft.AspNet.Http; 

using Microsoft.Extensions.DependencyInjection; 
using Microsoft.Extensions.Configuration;  

namespace FirstAppDemo { 
   public class Startup { 
      public Startup() { 
         var builder = new ConfigurationBuilder()   
            .AddJsonFile("AppSettings.json"); 
         Configuration = builder.Build(); 
      }  
      public IConfiguration Configuration { get; set; }  
      
      // This method gets called by the runtime. 
      // Use this method to add services to the container. 
      // For more information on how to configure your application, 
      // visit http://go.microsoft.com/fwlink/?LinkID=398940 
      public void ConfigureServices(IServiceCollection services) { 
      }  
      
      // This method gets called by the runtime.  
      // Use this method to configure the HTTP request pipeline. 
      public void Configure(IApplicationBuilder app) {
         app.UseIISPlatformHandler();  
         app.Run(async (context) => { 
            var msg = Configuration["message"]; 
            await context.Response.WriteAsync(msg); 
         });  
      }  
        
      // Entry point for the application. 
      public static void Main(string[] args) =7gt; WebApplication.Run<Startup>(args); 
   } 
}

现在让我们运行应用程序.运行应用程序后,它将生成以下输出.

运行应用程序