[英]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.