英文:
Registering MVC services in ASP .NET Core 6
问题
在ASP.NET Core 6之前,注册MVC服务是在启动类中完成的。但是现在在当前的ASP.NET Core空项目中没有启动类。
在启动类中,使用AddMvc()
方法和MVC中间件UseMvcWithDefaultRoute()
方法完成了这个操作。
如何在没有启动类的情况下实现这个呢?
尝试在网上搜索,但似乎大多数博客文章和YouTube视频都是在移除启动类之前制作的。并且找不到具体的文档。
任何帮助都将不胜感激,谢谢。
英文:
Before ASP.NET Core 6, registering MVC services was done in the startup class. But right now there is no startup class in the current ASP.NET Core empty project.
In the startup class, it was done in the ConfigureServices
method using AddMvc()
and the MVC middleware, UseMvcWithDefaultRoute()
was added in the Configure
method.
How do I implement this without the startup class?
Tried searching online, but it seems most blog posts and youtube videos were made before the removal of the startup class. And couldn't find the specific docs either.
Any help would be appreciated, thanks.
答案1
得分: 0
不需要启动类,ASP.NET Core应用程序使用Program.cs
文件来包含应用程序启动代码。
在Program.cs文件中:
-
配置应用程序所需的服务。
-
将应用程序的请求处理管道定义为一系列中间件组件。
您可以参考此处的代码,它实现了无需启动类的功能:
var builder = WebApplication.CreateBuilder(args);
// 将服务添加到容器中。
builder.Services.AddControllersWithViews();
var app = builder.Build();
...
// 定义您的中间件管道。
app.MapDefaultControllerRoute();
app.Run();
您可以阅读ASP.NET Core中的应用程序启动以获取更多信息。
英文:
> How do I implement this without the startup class?
ASP.NET Core apps created with the web templates contain the application startup code in the Program.cs
file.
The Program.cs file is where:
-
Services required by the app are configured.
-
The app's request handling pipeline is defined as a series of middleware components.
You can refer to the code shown here, it implements this without the startup class:
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllersWithViews();
var app = builder.Build();
...
// define your middleware pipeline
app.MapDefaultControllerRoute();
app.Run();
You can read App startup in ASP.NET Core to learn more.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论