1. 程式人生 > 實用技巧 >ASP.NET Core妙用分支路由MapWhen整合專案模型類賦值程式碼生成中介軟體

ASP.NET Core妙用分支路由MapWhen整合專案模型類賦值程式碼生成中介軟體

、使用場景

  1.屬性賦值

  2.物件初始化

像以上兩種情況,當屬性欄位較多,賦值就顯得繁瑣,這裡可以使用app.MapWhen()方法建立路由分支構建獨立無侵入式賦值程式碼生成。

二、專案/演示

  http://101.132.140.8:3613/codeIntelligencing

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

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllers();
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();

                // 開發環境下啟用
                app.UseCodeIntelligencing(typeof(UserEntity).Assembly, typeof(UserDTO).Assembly, typeof(UserModel).Assembly);
                //// 自定義路由分支
                //app.UseCodeIntelligencing("/custom_path", options => 
                //{
                //    options.Assemblies.Add(typeof(UserEntity).Assembly);
                //    options.Assemblies.Add(typeof(UserDTO).Assembly);
                //    options.Assemblies.Add(typeof(UserModel).Assembly);
                //});
            }
            app.UseHttpsRedirection();

            app.UseRouting();

            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });
        }
    }

  

三、原始碼下載

CodeIntelligencing