我试图完全理解依赖注入。我正在定义一个过滤器并想从配置文件中读取。在过滤器内部实例化配置是更好的做法,还是可以在全局范围内这样做,例如在启动时?如果是这样,有什么指示如何做到这一点?
public class CompanyFilter : ActionFilterAttribute
{
string _ERPUrl;
public CompanyFilter(IConfiguration iconfiguration)
{
ERPUrl = iconfiguration.GetSection("AppSettings").GetSection("ERPUrl").Value;
}
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
if (filterContext.Controller is Controller controller)
controller.ViewBag.ERPUrl = _ERPUrl;
//filterContext.Controller.ViewBag.Company = "Test";
}
}
创业班
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
....
控制器
namespace Projects.Controllers
{
[CompanyFilter]
public class HomeController : Controller
{
....
产生以下错误。
Controllers\HomeController.cs(14,6):错误 CS7036:没有给出与“CompanyFilter.CompanyFilter(IConfiguration)”所需的形式参数“iconfiguration”相对应的参数
偶然的你
宝慕林4294392
缥缈止盈
相关分类