ASP.NETCore5MVC中ConfigureServices方法去哪了?
创始人
2024-09-17 11:30:49
0

在ASP.NET Core 5 MVC中,不再需要使用ConfigureServices方法来配置依赖注入,而是直接在Program.cs中添加对服务的配置。下面是一个示例代码:

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

namespace MyProject
{
    public class Startup
    {
        public IConfiguration Configuration { get; }

        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public void ConfigureServices(IServiceCollection services)
        {
            // 添加服务
            services.AddMvc();
        }

        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            // 启用MVC中间件
            app.UseRouting();
            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllerRoute(
                    name: "default",
                    pattern: "{controller=Home}/{action=Index}/{id?}");
            });
        }
    }

    public class Program
    {
        public static void Main(string[] args)
        {
            CreateHostBuilder(args).Build().Run();
        }

        public static IHostBuilder CreateHostBuilder(string[] args)
        {
            return Host.CreateDefaultBuilder(args)
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder.UseStartup();
                });
        }
    }
}

在上述示例代码中,ConfigureServices方法被移除了,而把服务的配置添加到了Program.cs文件中的CreateHostBuilder方法里面。这样的重构可以更好地保持代码的简洁和可读性,同时更好地实现依赖注入的灵活性。

相关内容

热门资讯

每日必看!wepoker私人局... 每日必看!wepoker私人局规律,hhpoker辅助,一贯存在有挂(真是有挂)1、进入到是否有挂之...
推荐一款!aapoker脚本怎... 推荐一款!aapoker脚本怎么用,pokemmo手机辅助软件,一直是有挂(存在有挂)破解侠是真的助...
科普攻略!德州真人透视脚本,h... 科普攻略!德州真人透视脚本,hhpoker可以控制吗,果然有挂(有挂教学)1、实时透视辅助更新:用户...
专业讨论!来玩app 德州 辅... 专业讨论!来玩app 德州 辅助,wepoker新号好一点吗,一直真的有挂(有挂头条)1)免费钻石:...
一分钟快速了解!aapoker... 一分钟快速了解!aapoker透视方法,wejoker透视方法,真是是有挂(有挂教程);1、超多福利...
解密关于!wepoker有人用... 解密关于!wepoker有人用过吗,wepoker怎么开辅助,一贯存在有挂(今日头条)1、首先打开辅...
分享开挂内幕!wepoker新... 分享开挂内幕!wepoker新号好一点吗,德普之星辅助工具如何打开,一贯真的是有挂(有挂详细)1)辅...
终于知道!fishpoker透... 终于知道!fishpoker透视,德扑圈透视,竟然真的有挂(有挂功能)1、完成辅助器v3.3的残局,...
大神推荐!wepoker有没有... 大神推荐!wepoker有没有透视方法,wepoker一直输的号能继续打吗,一贯真的是有挂(有挂总结...
玩家必看科普!aapoker安... 玩家必看科普!aapoker安装包怎么使用,hhpoker软件可以玩吗,确实存在有挂(有挂攻略)1、...