当前位置:网站首页>In Net 6 CS more concise method

In Net 6 CS more concise method

2022-07-06 03:47:00 Dotnet cross platform

If you are following .NET 6, So you should know , stay .NET 6 In the project , No, Startup.cs file , Now we use Program.cs File to complete the unified configuration .

I previously posted an article using .NET 6 Project use Startup.cs The article .

stay .NET 6 Project use Startup.cs

Can it be further improved , Make it better ? Certainly. , Next , I'll show you a more concise approach .

To make the code look simpler , I used the extension method . stay .NET 6 in , Use WebApplicationBuilder Build the program . therefore , We are WebApplicationBuilder and WebApplication Create an extension method .

First , Create a static class RegisterServiceExtensions, Then define RegisterServices Static methods . And then put Program.cs The code of the registration service is migrated here , as follows

2e1b501ced1969fc211473c85bd4ee51.png

Next , We create another static class RegisterMiddlewareExtensions, Then define static methods RegisterServices, And put Program.cs The code of the registered middleware is migrated here .

adc4c03b9a3e2fdea98d3f955bf8297e.png

Last , Update the Program.cs Use these extension methods .

a58334836873e9b0978e031a8cc6f1e4.png

perhaps , Use one line of code directly to complete these tasks .

74a0e169ebcd77185240da23f404bd6e.png

Now? , Code looks simpler 、 Can be read , And easy to manage .

END

2bb76389c74013e7d1a571d2a601f056.gif

原网站

版权声明
本文为[Dotnet cross platform]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/187/202207060339049069.html