简体   繁体   English

Asp.Net Core 3 如何在 program.cs 文件中放入 function - 修饰符“private”对此项目无效

[英]Asp.Net Core 3 how to put a function in the program.cs file - The modifier 'private' is not valid for this item

The November 2021 release of AspNet Core 3 has a Program.cs file without any methods, you simply configure the services. 2021 年 11 月发布的 AspNet Core 3 有一个 Program.cs 文件,没有任何方法,您只需配置服务即可。

app.UseDefaultFiles();
app.UseStaticFiles();

app.Run();

However, if you need to add a method, eg:但是,如果您需要添加方法,例如:

public Task OnFallbackAsync(DelegateResult<HttpResponseMessage> response, Context context)
{
    Console.WriteLine("About to call the fallback action. This is a good place to do some logging");
    return Task.CompletedTask;
}

You get an error:你得到一个错误:

The modifier 'public' is not valid for this item修饰符“public”对此项目无效

How to declare functions in the Program.cs file?如何在 Program.cs 文件中声明函数?

The solution is to remove the scope keyword (Private, Internal, Public):解决方法是去掉 scope 关键字(Private、Internal、Public):

app.UseDefaultFiles();
app.UseStaticFiles();

app.Run();
    
Task OnFallbackAsync(DelegateResult<HttpResponseMessage> response, Context context)
{
    Console.WriteLine("About to call the fallback action. This is a good place to do some logging");
    return Task.CompletedTask;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何返回在 Program.cs ASP.Net core 6 中找不到 - How to return not found in Program.cs ASP.Net core 6 Asp.net core 6 Program.cs文件中appsettings.json的使用方法 - How to use appsettings.json in Asp.net core 6 Program.cs file ASP.NET 核心 Web API - 如何将 .NET 核心 5 中的 SetupSerilog 转换为 .NET 核心 6 Program.cs - ASP.NET Core Web API - How to convert SetupSerilog in .NET Core 5 to .NET Core 6 Program.cs ASP.NET 核心程序.cs配置 - ASP.NET Core program.cs configuration 避免在ASP.NET Core Program.cs中使用静态值 - Avoid static value in ASP.NET Core Program.cs 如何在 ASP.NET CORE 2.1 中的 Program.cs 文件中检查代码是在开发模式还是生产模式下运行? - How to check whether the code is running in development or Production mode in Program.cs file in ASP.NET CORE 2.1? 如何在 asp.net mvc 核心的 program.cs 中添加 pgadmin db - How to add pgadmin db in program.cs in asp.net mvc core 如何从 program.cs 中的 appsettings 中读取 UrlPrefixes - asp.net core 3.1 - how to read UrlPrefixes from appsettings in program.cs - asp.net core 3.1 如何从 ASP.NET Core 中的 Program.cs 访问 IWebHostEnvironment - How to access IWebHostEnvironment from Program.cs in ASP.NET Core ASP.NET Core 6 MVC:如何从 controller 访问 Program.cs 中定义的变量值? - ASP.NET Core 6 MVC : how to access a variable's value that is defined in Program.cs from a controller?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM