【開源】后台權限管理系統升級到aspnetcore3.1


 

netcore mvc快速開發系統(菜單,角色,權限[精確到按鈕])開源介紹

GitHub:https://github.com/yuzd/AntMgr

csporj文件修改

image

如上圖:

  • 將TargetFramework 由 【netcoreapp2.2】 變更成 【netcoreapp3.1】
  • 由於使用的是Razor渲染 新增 <AddRazorSupportForMvc>true</AddRazorSupportForMvc>

image

如上圖,刪除以下包,因為在aspnetcore3.1 sdk是自帶了

  • Microsoft.AspNetCore.App
  • Microsoft.AspNetCore.Razor.Design
  • Microsoft.VisualStudio.Web.CodeGeneration.Design

由於我使用了 NewtonsoftJson 新增

  • Microsoft.AspNetCore.Mvc.NewtonsoftJson 包

Program.cs文件里面的Host初始化代碼修改

老的方式:


        public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
                .UseStartup<Startup>()
                .ConfigureLogging()
                .UseNLog();
    }

新的方式如下面:

如果搭配autofac的話:

  • 需要添加最新版本的Autofac.Extensions.DependencyInjection
  • 如下新增 .UseServiceProviderFactory(new AutofacServiceProviderFactory())

    public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .UseServiceProviderFactory(new AutofacServiceProviderFactory()) //這里是autofac配合3.1的新方式
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder.UseStartup<Startup>()
                    .ConfigureLogging()
                    .UseNLog();
                });
    }

StartUp.cs文件變更

針對autofac的改動

下面是老的方式 返回一個 被autofac包裝的 IServiceProvider



public IServiceProvider ConfigureServices(IServiceCollection services)
{
    
    
}

下面是新的方式

        // 添加這個 是為了生成一個 容器 在別的地方使用
       public ILifetimeScope AutofacContainer { get; private set; }

       // 由於在 program.cs里面注冊了 autofac  factory 所以會走進來
        public void ConfigureContainer(ContainerBuilder builder)
        {
            //autofac打標簽模式 文檔:https://github.com/yuzd/Autofac.Annotation
            builder.RegisterModule(new AutofacAnnotationModule(
                    this.GetType().Assembly,
                    typeof(BaseRepository<>).Assembly,
                    typeof(HttpContext).Assembly)
                .SetAllowCircularDependencies(true)
                .SetDefaultAutofacScopeToInstancePerLifetimeScope());

        }
        

注冊mvc的方式改動

下面是老的方式:

  app.UseMvc(routes =>
    {
        // areas
        routes.MapRoute(
            name: "Admin",
            template: "{area:exists}/{controller=Home}/{action=Index}/{id?}");

        routes.MapRoute(
            name: "default",
            template: "{controller=Home}/{action=Index}/{id?}");
    });

下面是新的方式:


app.UseRouting();

app.UseCors();//注意 這個必須放在 中間

app.UseEndpoints(endpoints =>
{
    endpoints.MapControllers();
    endpoints.MapRazorPages();
    endpoints.MapAreaControllerRoute(
        name: "Admin", "Admin",
        pattern: "Admin/{controller=Home}/{action=Index}/{id?}");
    endpoints.MapControllerRoute(
        name: "default",
        pattern: "{controller=Home}/{action=Index}/{id?}");

});

用json需要另外配置下不然默認返回的第一個字母是小寫的

image

autofac容器想要在別的層使用

image


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM