ASP.NET Core项目相关配置的示例分析

发布时间:2021-09-17 10:42:35 作者:小新
来源:亿速云 阅读:110

这篇文章将为大家详细讲解有关ASP.NET Core项目相关配置的示例分析,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

我们将讨论 ASP.NET Core项目的相关的配置。在解决方案资源管理器中,您将看到 Startup.cs 文件。如果你有以前版本的 ASP.NET的工作经验,你可能希望看到一个 global.asax 文件,您可以在其中编写代码,它是一个编写程序启动时立即执行的代码的文件。

这里是 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!"); 
  }); 
 } 
 } 
}

在启动类中,我们的大部分工作将设计有两种方法。Configure 方法是构建HTTP处理管道的地方。

现在,我们有一个硬编码的字符串“Hello World !”来响应每个请求。我们不希望每个请求都是硬编码的字符串,我们想从一些组件加载响应字符串。

在解决方案资源管理器中,右键单击您的项目节点并选择Add→New Item。

ASP.NET Core项目相关配置的示例分析

在左侧窗格中,选择Installed → Code,然后在中间窗格中,选择JSON文件。给这个文件取名为AppSetting.json,并单击Add按钮如上面的截图。

ASP.NET Core项目相关配置的示例分析

让我们在AppSettings中添加以下代码。

{ 
 "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); 
 } 
}

让我们现在运行应用程序。一旦您运行该应用程序,它会产生下面的输出。

ASP.NET Core项目相关配置的示例分析

关于“ASP.NET Core项目相关配置的示例分析”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。

推荐阅读:
  1. ASP.NET Core中多语言支持的示例分析
  2. ASP.NET Core中新功能环境变量和启动设置的示例分析

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

asp.net core

上一篇:DevSecOps 调查得出60%的开发者发布代码速度增长了2倍的示例分析

下一篇:Asp.Net Core有什么优势

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》