从ASP.NET Core应用程序的根目录返回特定响应

时间:2022-11-24 11:12:50

I have created an ASP.NET Core app with some controllers which are working fine.

我创建了一个ASP.NET核心应用程序,其中一些控制器工作正常。

The routing system is structured like https://something.com/api/controller.

路由系统的结构类似于https://something.com/api/controller。

However, I am using the Always live option in Azure to keep the web app constantly active and not being paused while idle.

但是,我使用Azure中的Always live选项来保持Web应用程序始终处于活动状态,并且在空闲时不会暂停。

Problem is, every 5 minutes, Azure pings my app with the address https://something.com and comes back with a 404 error which is logged in my Application Insights report.

问题是,每隔5分钟,Azure就会使用地址https://something.com ping我的应用程序,并返回404错误,该错误记录在我的Application Insights报告中。

I would like to know how can I handle the requests made to the root of my app and return a 200 HTTP result instead.

我想知道如何处理对我的应用程序的根目录发出的请求,并返回200 HTTP结果。

Here is my Startup class:

这是我的Startup类:

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

1 个解决方案

#1


1  

Alright, it is in fact insanely simple:

好吧,它实际上非常简单:

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

#1


1  

Alright, it is in fact insanely simple:

好吧,它实际上非常简单:

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