Missing code in MS VS 2022 ASP .NET core WebApp

JerryM 1,116 Reputation points
2022-03-31T09:30:08.74+00:00

Hi,
I noticed, that new "MS VS 2022 ASP .NET core WebApp" has modified the "Program.cs" file:

var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();

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

app.Run();

there are no namespaces, no methods, no classes ... how can I go back to old style and display all missing code ???

Thanks
Jerry

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,189 questions
{count} votes

5 answers

Sort by: Most helpful
  1. SurferOnWww 1,911 Reputation points
    2022-03-31T12:21:22.49+00:00

    This also applies to .NET 6.0 ASP.NET Core app:

    .NET 6 C# console app template generates top-level statements
    https://learn.microsoft.com/en-us/dotnet/core/tutorials/top-level-templates

    0 comments No comments

  2. JerryM 1,116 Reputation points
    2022-03-31T14:00:23.397+00:00

    Yes, C# WPF .NET 6 generates only top-level statements.
    ASP C# .NET 6 generates also only top-level statements.
    The biggest problem is that there is also missing "startup.cs" file.
    As you can see below - the older version vs the newer one .... how can I display all missing lines and files in the
    newer version ? Where they are ?

    188739-asp-net5.jpg

    188717-asp-net6.jpg

    0 comments No comments

  3. Bruce (SqlWork.com) 56,846 Reputation points
    2022-03-31T22:50:21.767+00:00

    startup was just two methods called by the app builder.

     Host.CreateDefaultBuilder(args)  
           .ConfigureWebHostDefaults(webBuilder => 
                    { 
                        webBuilder.UseStartup<Startup>(); 
                    }); 
    

    a method to register services and a method to add middleware. these are just done inline. as endpoint routing is default, no need to configure. you can add it back, but why? startup was just to make it look like the old asp.net

    var builder = WebApplication.CreateBuilder(args); 
    
    // register services here old - startup.Configuration() 
    
    builder.Services.AddScoped<IMyService, MyService>(); 
    
    var app = builder.Build(); 
    
    // register middleware - old startup.Configure()  
    
    app.UseStaticFiles(); 
    app.MapGet("/", () => "Hello World!"); 
    
     app.Run(); 
    
    0 comments No comments

  4. SurferOnWww 1,911 Reputation points
    2022-04-01T00:31:47.74+00:00

    The following Microsoft document might be helpful. See the sections "New hosting model" and below.

    Migrate from ASP.NET Core 5.0 to 6.0
    https://learn.microsoft.com/en-us/aspnet/core/migration/50-to-60?view=aspnetcore-6.0&tabs=visual-studio

    0 comments No comments

  5. Zhi Lv - MSFT 32,016 Reputation points Microsoft Vendor
    2022-04-01T07:24:24.887+00:00

    Hi @JerryM ,

    For example, we create a Asp.net Core Web App (MVC, Asp.Net 6), and the program.cs like this:
    189007-image.png

    To add back the Startup Class to ASP.NET Core 6, we can right click the project and add a Startup.cs file, and add its content based on the Program.cs file content. Like this:

    188970-image.png

    You can view the source code from here:

    189036-startupcs.txt

    189008-programcs.txt

    After running the application, it works well:

    188976-image.png

    More detail information about the code samples migrating from Asp.net 5 to Asp.net 6, see Code samples migrated to the new minimal hosting model in ASP.NET Core 6.0.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best regards,
    Dillion

    0 comments No comments