Registering MVC services in ASP .NET Core 6 在ASP.NET Core 6中注册MVC服务

huangapple go评论46阅读模式
英文:

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文件中:

  1. 配置应用程序所需的服务。

  2. 将应用程序的请求处理管道定义为一系列中间件组件。

您可以参考此处的代码,它实现了无需启动类的功能:

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:

  1. Services required by the app are configured.

  2. 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.

huangapple
  • 本文由 发表于 2023年5月15日 02:48:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/76249159.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定