Throwing an error CS1022: Type or
namespace definition, or end-of-file expected
What I have tried:
Program.cs
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;
11
namespace
BookListRazor
13
public
class
Program
15
public
static
void
Main(string[] args)
17
CreateHostBuilder(args).Build().Run();
20
public
static
IHostBuilder CreateHostBuilder(string[] args) =>
21
Host.CreateDefaultBuilder(args)
22
.ConfigureWebHostDefaults(webBuilder =>
24
webBuilder.UseStartup<Startup>();
25
});
32
var
builder = WebApplication.CreateBuilder(args);
34
35
builder.Services.AddRazorPages();
37
var
app = builder.Build();
39
40
if
(!app.Environment.IsDevelopment())
42
app.UseExceptionHandler(
"
/Error"
);
43
44
app.UseHsts();
47
app.UseHttpsRedirection();
48
app.UseStaticFiles();
50
app.UseRouting();
52
app.UseAuthorization();
54
app.MapRazorPages();
56
app.Run();
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.
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
13
public
class
Program
15
public
static
void
Main(string[] args)
17
CreateHostBuilder(args).Build().Run();
20
public
static
IHostBuilder CreateHostBuilder(string[] args) =>
21
Host.CreateDefaultBuilder(args)
22
.ConfigureWebHostDefaults(webBuilder =>
24
webBuilder.UseStartup<Startup>();
25
});
32
var
builder = WebApplication.CreateBuilder(args);
34
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);
builder.Services.AddRazorPages();
Read the question carefully.
Understand that English isn't everyone's first language so be lenient of bad
spelling and grammar.
If a question is poorly phrased then either ask for clarification, ignore it, or
edit the question
and fix the problem. Insults are not welcome.
Don't tell someone to read the manual. Chances are they have and don't get it.
Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.