最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

ASP.NET Core項目配置教程(6)

 更新時間:2017年06月15日 11:50:02   作者:卿文剛  
這篇文章主要為大家詳細介紹了ASP.NET Core項目配置教程,具有一定的參考價值,感興趣的小伙伴們可以參考一下

在這一章,我們將討論 ASP.NET Core項目的相關的配置。在解決方案資源管理器中,您將看到 Startup.cs 文件。如果你有以前版本的 ASP.NET的工作經(jīng)驗,你可能希望看到一個 global.asax 文件,您可以在其中編寫代碼,它是一個編寫程序啟動時立即執(zhí)行的代碼的文件。

  • 你可能也希望看到一個 web.config 文件,該文件包含您的應用程序執(zhí)行所需的所有配置參數(shù)。
  • 在 ASP.NET Core中,那些文件都沒了,取而代之的是 Startup.cs文件.
  • Startup.cs里面是一個啟動類文件,并在該類中您可以配置您的應用程序甚至配置您的配置資源。

這里是 Startup.cs 文件中的默認實現(xiàn)代碼:

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 方法是構(gòu)建HTTP處理管道的地方。

  • 這定義了應用程序如何響應請求。目前該應用程序只能說“Hello World!”如果我們希望該應用程序具有不同的行為,我們需要通過添加額外的代碼到這個Configure方法中來改變周圍的管道。
  • 例如,如果我們想要提供一個 index.html 文件的靜態(tài)文件,我們將需要在Configure方法中添加一些代碼。
  • 你也可以有一個錯誤頁面或Asp.Net Controller的異常請求的路由;這兩個場景還需要在這個配置方法中做一些工作。
  • 在啟動類中,您還將看到 ConfigureServices() 方法。這可幫助您配置您的應用程序的組件。

現(xiàn)在,我們有一個硬編碼的字符串“Hello World !”來響應每個請求。我們不希望每個請求都是硬編碼的字符串,我們想從一些組件加載響應字符串。

  • 其他組件可能會從數(shù)據(jù)庫加載文本,或從一個web服務或一個JSON文件,我們不管這它是從什么地方加載。
  • 我們會設置一個場景,這樣我們就沒有這個硬編碼字符串了。

在解決方案資源管理器中,右鍵單擊您的項目節(jié)點并選擇Add→New Item。

在左側(cè)窗格中,選擇Installed → Code,然后在中間窗格中,選擇JSON文件。給這個文件取名為AppSetting.json,并單擊Add按鈕如上面的截圖。

讓我們在AppSettings中添加以下代碼。

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

現(xiàn)在我們需要從 Startup.cs 文件訪問此消息。這里是 Startup.cs 文件從 JSON 文件閱讀上面的消息的實現(xiàn)代碼。

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

讓我們現(xiàn)在運行應用程序。一旦您運行該應用程序,它會產(chǎn)生下面的輸出。

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

最新評論

墨竹工卡县| 迁西县| 平安县| 汶上县| 通州市| 若羌县| 麻阳| 海晏县| 兴国县| 元朗区| 天等县| 通州区| 石渠县| 南宫市| 尉氏县| 乐东| 海兴县| 铜山县| 永丰县| 勐海县| 柏乡县| 姜堰市| 绥阳县| 丹巴县| 仪陇县| 长武县| 通江县| 龙陵县| 邵东县| 绥滨县| 房山区| 凯里市| 炎陵县| 富阳市| 兴宁市| 渝中区| 资溪县| 鄱阳县| 普兰县| 南丰县| 大悟县|