[英]Return a specific response from the root of an ASP.NET Core app
我創建了一個 ASP.NET Core 應用程序,其中包含一些運行良好的控制器。
路由系統的結構類似於https://something.com/api/controller 。
但是,我使用 Azure 中的“始終在線”選項來保持 Web 應用程序始終處於活動狀態,並且不會在空閑時暫停。
問題是,每隔 5 分鍾,Azure 就會使用地址https://something.com ping 我的應用程序,並返回 404 錯誤,該錯誤記錄在我的 Application Insights 報告中。
我想知道如何處理對我的應用程序根目錄發出的請求並返回 200 HTTP 結果。
這是我的啟動類:
public class Startup
{
public Startup(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
.AddEnvironmentVariables();
Configuration = builder.Build();
}
private IConfigurationRoot Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
// Add framework services.
services.AddMvc();
services.AddSingleton<ICachingService>(e => new CachingService(Configuration["Redis:ConnectionString"]));
var loggingService = new LoggingService(Configuration["ApplicationInsights:InstrumentationKey"]);
services.AddSingleton(typeof(ILoggingService), loggingService);
}
// 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(Configuration.GetSection("Logging"));
loggerFactory.AddDebug();
app.UseMvc();
}
}
好吧,它實際上非常簡單:
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
app.UseMvc();
app.Run(async context =>
{
await context.Response.WriteAsync("API"); // returns a 200 with "API" as content.
});
}
public void Configure(IApplicationBuilder app)
{
app.UseEndpoints(endpoints =>
{
endpoints.MapGet("/", (context) => context.Response.WriteAsync("Success"));
});
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.