当前位置:网站首页>在 .NET 6 中使用 Startup.cs 更简洁的方法
在 .NET 6 中使用 Startup.cs 更简洁的方法
2022-07-06 03:39:00 【dotNET跨平台】
如果您在关注 .NET 6,那么您应该知道,在 .NET 6 项目中,没有 Startup.cs 文件,现在使用了 Program.cs 文件来完成统一的配置。
我之前发了一篇使用在 .NET 6 项目中使用 Startup.cs 的文章。
能否能进一步改进,让它变得更好呢?当然可以,接下来,我会展示一种更为简洁的方法。
为了让代码看上去更简洁,我使用了扩展方法。在 .NET 6 中,使用WebApplicationBuilder 构建程序。所以,我们为 WebApplicationBuilder 和 WebApplication 创建扩展方法。
首先,创建一个静态类 RegisterServiceExtensions,然后定义 RegisterServices 静态方法。然后把 Program.cs 里面注册服务的代码迁移到这里,如下

接下来,我们创建另外一个静态类 RegisterMiddlewareExtensions, 然后定义静态方法 RegisterServices,并把 Program.cs 里面的注册中间件的代码迁移到这里。

最后,更新项目的 Program.cs 使用这些扩展方法。

或者,直接使用一行代码完成这些工作。

现在,代码看起来更简洁、可读,且易于管理。
END

边栏推荐
- Codeforces Global Round 19
- 【RISC-V】外部中断
- [Li Kou] the second set of the 280 Li Kou weekly match
- [prediction model] difference method model
- Crazy, thousands of netizens are exploding the company's salary
- 【SLAM】lidar-camera外参标定(港大MarsLab)无需二维码标定板
- MySQL Server层四个日志
- RT thread -- FTP of LwIP (2)
- canvas切积木小游戏代码
- How to write compile scripts compatible with arm and x86 (Makefile, cmakelists.txt, shell script)
猜你喜欢

Flask learning and project practice 8: introduction and use of cookies and sessions

The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower

Canvas cut blocks game code

Remote Sensing Image Super-resolution and Object Detection: Benchmark and State of the Art

RT thread -- FTP of LwIP (2)

2.2 STM32 GPIO操作

给新人工程师组员的建议

Getting started with applet cloud development - getting user search content

施努卡:视觉定位系统 视觉定位系统的工作原理

Recommended papers on remote sensing image super-resolution
随机推荐
[padding] an error is reported in the prediction after loading the model weight attributeerror: 'model' object has no attribute '_ place‘
Brush questions in summer -day3
2.13 weekly report
Lua uses require to load the shared library successfully, but the return is Boolean (always true)
Serial port-rs232-rs485-ttl
Schnuka: what is visual positioning system and how to position it
[rust notes] 18 macro
Codeforces Global Round 19
An article will give you a comprehensive understanding of the internal and external components of "computer"
Cubemx transplantation punctual atom LCD display routine
【SLAM】ORB-SLAM3解析——跟踪Track()(3)
MPLS experiment
User perceived monitoring experience
[practical exercise] face location model based on skin color
Yyds dry inventory what is test driven development
指针笔试题~走近大厂
JS Vanke banner rotation chart JS special effect
[slam] lidar camera external parameter calibration (Hong Kong University marslab) does not need a QR code calibration board
2、GPIO相关操作
EDCircles: A real-time circle detector with a false detection control 翻译