Search code examples
c#asp.net-core.net-6.0visual-studio-2022

Why does a new project created using Visual Studio 2022 and the ASP.NET Core web app (Model, View, Controller) template not build?


I created a new project using Visual Studio 2022 and selected the template "ASP.NET Core Web App (Model-View-Controller)" but the project created won't build and I get the following error:

Error CS5001
Program does not contain a static 'Main' method suitable for an entry point

Program.cs

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddControllersWithViews();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
    app.UseExceptionHandler("/Home/Error");
    // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
    app.UseHsts();
}

app.UseHttpsRedirection();
app.UseStaticFiles();

app.UseRouting();

app.UseAuthorization();

app.MapControllerRoute(
    name: "default",
    pattern: "{controller=Home}/{action=Index}/{id?}");

app.Run();

I tried removing the Program.cs file which did not fix it. I also tried creating a project from the "ASP.NET Core Web App" template which had the same issue.


Solution

  • I was able to determine that the cause of the error was illegal characters in the file path.

    My project was in a parent folder that had been checked out from a TFS git repository. The TFS project had spaces in the name which on checkout were converted to %20. I was able to build and run the project after renaming the folder.

    Original file path: My%20Git%20Repo/Projectname/ New file path: MyGitRepo/Projectname/