.net core 6.0 mvc项目
1、.net core 6.0添加startup文件
在 .NET 6 项目中使用 Startup.cs - 腾讯云开发者社区-腾讯云 (tencent.com)
using SystemProjectOne; var builder = WebApplication.CreateBuilder(args); //设置配置源 var startup = new Startup(builder.Configuration); //ConfigureServices主要是配置依赖注入(DI)。 startup.ConfigureServices(builder.Services); var app = builder.Build(); startup.Configure(app, builder.Environment); app.Run();
//新建Startup参考:https://zhuanlan.zhihu.com/p/537282897 //https://www.cnblogs.com/stulzq/p/7845026.html using Microsoft.AspNetCore.Builder; namespace SystemProjectOne { public class Startup { public Startup(IConfiguration configuration) { Configuration = configuration; } public IConfiguration Configuration { get; } //ConfigureServices方法中配置内置容器的服务 //https://www.cnblogs.com/stulzq/p/7761128.html public void ConfigureServices(IServiceCollection services) { #region 注入自定义Service服务 #endregion services.AddRazorPages(); } public void Configure(WebApplication app, IWebHostEnvironment env) { if (!app.Environment.IsDevelopment()) { app.UseExceptionHandler("/Error"); app.UseHsts(); } app.UseHttpsRedirection(); app.UseStaticFiles(); app.UseRouting(); app.UseAuthorization(); app.MapRazorPages(); } } }
2、.net core 6.0自动注入自定义service服务

采用读取json配置的方式自动注入service服务
第一:参考博客读取json文件配置内容:c#读取json文件配置内容并转为list - じ逐梦 - 博客园 (cnblogs.com)
注意:json配置文件存储的名字为类库的名字


第二:获取配置后的service后选用反射方式注入
在startup中注入

//ConfigureServices方法中配置内置容器的服务 //https://www.cnblogs.com/stulzq/p/7761128.html public void ConfigureServices(IServiceCollection services) { #region 注入自定义Service服务 BHP引擎 //配置文件中读取注入类 List<UsBhpItem> bhpItems = UsServiceAdd.Us_BHP_GetConfig(); //自动注入 UsServiceAdd.ServiceDiAdd(services, bhpItems); #endregion services.AddRazorPages(); }
public static class UsServiceAdd { #region 2.自动注入服务类 public static IServiceCollection ServiceDiAdd(this IServiceCollection services, List<UsBhpItem> bhplist) { IServiceCollection services1 = null; for (var i = 0; i < bhplist.Count; i++) { //不注入基类 if (!bhplist[i].IfBaseService) { bhplist[i].BaseServiceName = "absege"; } services1 = ServiceDiAdd(services, bhplist[i].PlugDLLScoped, bhplist[i].BaseServiceName, bhplist[i].InJectType); } return services1; } #endregion #region 3. 通过反射自动注入Di的扩展方法 /// <summary> /// 通过反射自动注入Di的扩展方法 /// </summary> /// <param name="services"></param> /// <param name="serviceAssembly">需要注入的程序集</param> /// <param name="baseServiceName">需要注入的类的基类, 如果没有指定基类,默认注入类库下面所有的类</param> ///<param name="InJectType">注入类型</param> /// <returns></returns> private static IServiceCollection ServiceDiAdd(this IServiceCollection services, string serviceAssembly, string? baseServiceName = null, InJectType InJectType = InJectType.Scope) { try { //读配置文件获取service程序集名称 Assembly assembly = Assembly.Load(serviceAssembly); //获取程序集下所有类 Type[] types = assembly.GetTypes(); types.ToList().ForEach(t => { if (string.IsNullOrEmpty(baseServiceName) || (t.BaseType != null && t.BaseType.ToString() == baseServiceName)) { if (InJectType == InJectType.Scope) { services.AddScoped(t); } else if (InJectType == InJectType.Single) { services.AddSingleton(t); } else if (InJectType == InJectType.Transient) { services.AddTransient(t); } } }); return services; } catch (Exception ex) { var mess = ex; throw; } } #endregion }
会引发出现一个错误:

原因:没有在项目中引用 SystemProjectOne.Repository(要注入的类) ,引用这个错误解决

参考文件.netCore 反射 :Could not load file or assembly 系统找不到指定文件 - mengtree - 博客园 (cnblogs.com)
3、asp.net core mvc 设置区域
添加区域

在startup文件夹中编辑路径(在其Configure方法中编辑)
//添加路由 app.UseRouting(); app.UseEndpoints(endpoints => { endpoints.MapControllerRoute( name: "areas", pattern: "{area:exists}/{controller=UsMain}/{action=Index}/{id?}"); endpoints.MapControllerRoute( name: "default", pattern: "{controller=SystemBase}/{action=Index}/{id?}"); //默认打开页面路径 }); app.UseMvc(routes => { routes.MapRoute( name: "default", template: "{action}/{id}", defaults: new {controller = "SystemBase", action = "Index", id = "" } //默认打开页面路径 ); });
asp.net core mvc 设置区域中的控制器为默认页面

参考文献:https://blog.csdn.net/puzi0315/article/details/118487004
本文来自博客园,作者:じ逐梦,转载请注明原文链接:https://www.cnblogs.com/ZhuMeng-Chao/p/17252191.html

浙公网安备 33010602011771号