php asp.net core,asp.net core实例教程之配置
Asp.Net Core-配置Asp.Net Core-配置
在這一章,我們將討論 ASP.NET Core項(xiàng)目的相關(guān)的配置。在解決方案資源管理器中,您將看到 Startup.cs 文件。如果你有以前版本的 ASP.NET的工作經(jīng)驗(yàn),你可能希望看到一個(gè) global.asax 文件,您可以在其中編寫(xiě)代碼,它是一個(gè)編寫(xiě)程序啟動(dòng)時(shí)立即執(zhí)行的代碼的文件。你可能也希望看到一個(gè) web.config 文件,該文件包含您的應(yīng)用程序執(zhí)行所需的所有配置參數(shù)。
在 ASP.NET Core中,那些文件都沒(méi)了,取而代之的是 Startup.cs文件.
Startup.cs里面是一個(gè)啟動(dòng)類文件,并在該類中您可以配置您的應(yīng)用程序甚至配置您的配置資源。
這里是 Startup.cs 文件中的默認(rèn)實(shí)現(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!");
});
}
}
}
在啟動(dòng)類中,我們的大部分工作將設(shè)計(jì)有兩種方法。Configure 方法是構(gòu)建HTTP處理管道的地方。這定義了應(yīng)用程序如何響應(yīng)請(qǐng)求。目前該應(yīng)用程序只能說(shuō)“Hello World!”如果我們希望該應(yīng)用程序具有不同的行為,我們需要通過(guò)添加額外的代碼到這個(gè)Configure方法中來(lái)改變周?chē)墓艿馈?/p>
例如,如果我們想要提供一個(gè) index.html 文件的靜態(tài)文件,我們將需要在Configure方法中添加一些代碼。
你也可以有一個(gè)錯(cuò)誤頁(yè)面或Asp.Net Controller的異常請(qǐng)求的路由;這兩個(gè)場(chǎng)景還需要在這個(gè)配置方法中做一些工作。
在啟動(dòng)類中,您還將看到 ConfigureServices() 方法。這可幫助您配置您的應(yīng)用程序的組件。
現(xiàn)在,我們有一個(gè)硬編碼的字符串“Hello World !”來(lái)響應(yīng)每個(gè)請(qǐng)求。我們不希望每個(gè)請(qǐng)求都是硬編碼的字符串,我們想從一些組件加載響應(yīng)字符串。其他組件可能會(huì)從數(shù)據(jù)庫(kù)加載文本,或從一個(gè)web服務(wù)或一個(gè)JSON文件,我們不管這它是從什么地方加載。
我們會(huì)設(shè)置一個(gè)場(chǎng)景,這樣我們就沒(méi)有這個(gè)硬編碼字符串了。
在解決方案資源管理器中,右鍵單擊您的項(xiàng)目節(jié)點(diǎn)并選擇Add→New Item。
在左側(cè)窗格中,選擇Installed → Code,然后在中間窗格中,選擇JSON文件。給這個(gè)文件取名為AppSetting.json,并單擊Add按鈕如上面的截圖。
讓我們?cè)贏ppSettings中添加以下代碼。{
"message": "Hello, World! this message is from configuration file..."
}
現(xiàn)在我們需要從 Startup.cs 文件訪問(wèn)此消息。這里是 Startup.cs 文件從 JSON 文件閱讀上面的消息的實(shí)現(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(args);
}
}
讓我們現(xiàn)在運(yùn)行應(yīng)用程序。一旦您運(yùn)行該應(yīng)用程序,它會(huì)產(chǎn)生下面的輸出。
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來(lái)咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)總結(jié)
以上是生活随笔為你收集整理的php asp.net core,asp.net core实例教程之配置的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 每天耗电0.66度 小米米家三门冰箱21
- 下一篇: 早报:起亚高管怒斥比亚迪雇水军 本轮油价