Click here to Skip to main content
15,886,857 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Throwing an error CS1022: Type or namespace definition, or end-of-file expected

What I have tried:

Program.cs
C#
  1  using System;
  2  using System.Collections.Generic;
  3  using System.Linq;
  4  using System.Threading.Tasks;
  5  using Microsoft.AspNetCore.Hosting;
  6  using Microsoft.Extensions.Configuration;
  7  using Microsoft.Extensions.Hosting;
  8  using Microsoft.Extensions.Logging;
  9  
 10  
 11  namespace BookListRazor
 12  {
 13      public class Program
 14      {
 15          public static void Main(string[] args)
 16          {
 17              CreateHostBuilder(args).Build().Run();
 18          }
 19  
 20          public static IHostBuilder CreateHostBuilder(string[] args) =>
 21              Host.CreateDefaultBuilder(args)
 22                  .ConfigureWebHostDefaults(webBuilder =>
 23                  {
 24                      webBuilder.UseStartup<Startup>();
 25                  });
 26      }
 27  }
 28  
 29  
 30  
 31  
 32  var builder = WebApplication.CreateBuilder(args);
 33  
 34  // Add services to the container.
 35  builder.Services.AddRazorPages();
 36  
 37  var app = builder.Build();
 38  
 39  // Configure the HTTP request pipeline.
 40  if (!app.Environment.IsDevelopment())
 41  {
 42      app.UseExceptionHandler("/Error");
 43      // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
 44      app.UseHsts();
 45  }
 46  
 47  app.UseHttpsRedirection();
 48  app.UseStaticFiles();
 49  
 50  app.UseRouting();
 51  
 52  app.UseAuthorization();
 53  
 54  app.MapRazorPages();
 55  
 56  app.Run();
Posted
Updated 15-Aug-22 3:55am
v2

At a guess, you're targeting .NET 6, and using the new top-level statements feature:
Top-level statements - C# tutorial | Microsoft Docs[^]

However, as the documentation says:
A file with top-level statements can also contain namespaces and type definitions, but they must come after the top-level statements.
You have added classes and methods before the top-level statements, which won't work.

You have also defined two entry-points: the top-level statements, and the Program.Main method.

It looks like you've tried to combine two lots of code by pasting the code from one application into the code from another, without actually understanding how either application is structured.
 
Share this answer
 
v2
Comments
Richard MacCutchan 15-Aug-22 10:02am    
I totally forgot about that, even though I only read about it quite recently.
The BookListRazor namespace is closed at line 27. So everything from that point on is orphaned code. That is to say, it has no namespace or class to contain it. A simple glance at the code makes this clear.
 
Share this answer
 
To add to what Richard has - correctly - said ...

In C# everything is part of a class, there is no "global code" or "global methods".
So when you write code like this:
namespace BookListRazor
12  {
13      public class Program
14      {
15          public static void Main(string[] args)
16          {
17              CreateHostBuilder(args).Build().Run();
18          }
19
20          public static IHostBuilder CreateHostBuilder(string[] args) =>
21              Host.CreateDefaultBuilder(args)
22                  .ConfigureWebHostDefaults(webBuilder =>
23                  {
24                      webBuilder.UseStartup<Startup>();
25                  });
26      }
27  }
28
29
30
31
32  var builder = WebApplication.CreateBuilder(args);
33
34  // Add services to the container.
35  builder.Services.AddRazorPages();
The two close curly brackets on lines 26 and 27 close the Program class and the BookListRazor namespace respectively, and are outside of any class (and indeed namespace).
As such, the lines of code after them won't compile - they cannot be accessed at all so the compiler refuses to have anything to do with them.

In addition, even if you moved them into the class, they still wouldn't compile because with the exception of field or property definitions all executable code code must be contained in a method.

I assume that they should actually be a part of the CreateHostBuilder method, but since you have used a short form method definition you would need to change that to a curly bracket form instead:
namespace BookListRazor
    {
    public class Program
        {
        public static void Main(string[] args)
            {
            CreateHostBuilder(args).Build().Run();
            }

        public static IHostBuilder CreateHostBuilder(string[] args)
            {
            Host.CreateDefaultBuilder(args)
                .ConfigureWebHostDefaults(webBuilder => webBuilder.UseStartup<Startup>());

            var builder = WebApplication.CreateBuilder(args);

            // Add services to the container.
            builder.Services.AddRazorPages();
            ...
            }
        }
    }
 
Share this answer
 
Comments
Richard MacCutchan 13-Aug-22 10:37am    
The difference between your answer and mine is obviously that you have a fan. :)
OriginalGriff 13-Aug-22 11:10am    
:cool:
Richard Deeming 15-Aug-22 9:56am    
there is no "global code" or "global methods"

Not entirely true any more - see Top-level statements[^] in the MS docs. :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900