Skip to content

'Do not use top level statements' check does not work for Minimal API template. #41636

Closed
@TanvirArjel

Description

@TanvirArjel

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

First of all, thank you ASP.NET Core Team for adding an option in templates to avoid top-level statements.

However, we have noticed that do not use top-level statements check is not working for the Minimal API template.

During Project Creation:

Capture3

Generated Code in Program.cs file:

using Microsoft.AspNetCore.OpenApi;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}

app.UseHttpsRedirection();

var summaries = new[]
{
    "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};

app.MapGet("/weatherforecast", () =>
{
    var forecast = Enumerable.Range(1, 5).Select(index =>
        new WeatherForecast
        (
            DateTime.Now.AddDays(index),
            Random.Shared.Next(-20, 55),
            summaries[Random.Shared.Next(summaries.Length)]
        ))
        .ToArray();
    return forecast;
})
.WithName("GetWeatherForecast")
.WithOpenApi();

app.Run();

internal record WeatherForecast(DateTime Date, int TemperatureC, string? Summary)
{
    public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
}

Expected Behavior

The code should be wrapped inside explicit Program.Main.

Steps To Reproduce

No response

Exceptions (if any)

No response

.NET Version

6.0.300 and 7.0.100-preview.4.22252.9

Anything else?

Visual Studio Version: 2022 17.3 preview 1.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions