从ASP.NET核心应用程序的根返回特定响应

x759pob2  于 2023-02-26  发布在  .NET
关注(0)|答案(3)|浏览(143)

我已经创建了一个ASP.NET核心应用程序与一些控制器,这是工作正常。
路由系统的结构类似于**https://something.com/api/controller
但是,我正在Azure中使用
Always live选项来保持Web应用始终处于活动状态,并且在空闲时不会暂停。
问题是,每隔5分钟,Azure会使用地址
https://something.com**ping我的应用,并返回404错误,该错误记录在我的应用洞察报告中。
我想知道如何处理对应用根目录的请求并返回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();
    }
}
2ledvvac

2ledvvac1#

好吧,事实上非常简单:

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

au9on6nz2#

public void Configure(IApplicationBuilder app)
{
   app.UseEndpoints(endpoints =>
   {
      endpoints.MapGet("/", (context) => context.Response.WriteAsync("Success"));
   });
}
gstyhher

gstyhher3#

app.MapGet("/", () => "Hello Server!");

这在.net 7上工作

相关问题