您好,登錄后才能下訂單哦!
這篇文章主要介紹了ASP.NET Core項目如何配置一個文件包含應(yīng)用程序執(zhí)行所需的所有配置參數(shù),具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
你可能也希望看到一個 web.config 文件,該文件包含您的應(yīng)用程序執(zhí)行所需的所有配置參數(shù)。
在 ASP.NET Core中,那些文件都沒了,取而代之的是 Startup.cs文件.
Startup.cs里面是一個啟動類文件,并在該類中您可以配置您的應(yīng)用程序甚至配置您的配置資源。
這里是 Startup.cs 文件中的默認(rèn)實現(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!"); }); } } }
在啟動類中,我們的大部分工作將設(shè)計有兩種方法。Configure 方法是構(gòu)建HTTP處理管道的地方。
這定義了應(yīng)用程序如何響應(yīng)請求。目前該應(yīng)用程序只能說“Hello World!”如果我們希望該應(yīng)用程序具有不同的行為,我們需要通過添加額外的代碼到這個Configure方法中來改變周圍的管道。
例如,如果我們想要提供一個 index.html 文件的靜態(tài)文件,我們將需要在Configure方法中添加一些代碼。
你也可以有一個錯誤頁面或Asp.Net Controller的異常請求的路由;這兩個場景還需要在這個配置方法中做一些工作。
在啟動類中,您還將看到 ConfigureServices() 方法。這可幫助您配置您的應(yīng)用程序的組件。
現(xiàn)在,我們有一個硬編碼的字符串“Hello World !”來響應(yīng)每個請求。我們不希望每個請求都是硬編碼的字符串,我們想從一些組件加載響應(yīng)字符串。
其他組件可能會從數(shù)據(jù)庫加載文本,或從一個web服務(wù)或一個JSON文件,我們不管這它是從什么地方加載。
我們會設(shè)置一個場景,這樣我們就沒有這個硬編碼字符串了。
在解決方案資源管理器中,右鍵單擊您的項目節(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)在運行應(yīng)用程序。一旦您運行該應(yīng)用程序,它會產(chǎn)生下面的輸出。
感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“ASP.NET Core項目如何配置一個文件包含應(yīng)用程序執(zhí)行所需的所有配置參數(shù)”這篇文章對大家有幫助,同時也希望大家多多支持億速云,關(guān)注億速云行業(yè)資訊頻道,更多相關(guān)知識等著你來學(xué)習(xí)!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。