Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cocoa/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc Autofac能否将依赖项注入布局视图文件?_Asp.net Mvc_Dependency Injection_Asp.net Mvc 4_Autofac - Fatal编程技术网

Asp.net mvc Autofac能否将依赖项注入布局视图文件?

Asp.net mvc Autofac能否将依赖项注入布局视图文件?,asp.net-mvc,dependency-injection,asp.net-mvc-4,autofac,Asp.net Mvc,Dependency Injection,Asp.net Mvc 4,Autofac,我试图在共享布局视图页面中注入依赖项,以避免在使用该布局的每个视图中都必须这样做 我在wiki中遵循了将依赖项注入视图的方法,但该属性始终为null Autofac能否将属性注入到作为布局文件的自定义视图页面中 这是我的设置。 自定义视图页面 namespace MyApp { using System.Web.Mvc; public abstract class CustomViewPage : WebViewPage { public IHelper

我试图在共享布局视图页面中注入依赖项,以避免在使用该布局的每个视图中都必须这样做

我在wiki中遵循了将依赖项注入视图的方法,但该属性始终为null

Autofac能否将属性注入到作为布局文件的自定义视图页面中

这是我的设置。 自定义视图页面

namespace MyApp
{
    using System.Web.Mvc;

    public abstract class CustomViewPage : WebViewPage
    {
        public IHelper Helper { get; set; }
    }
}
~/Views/Shared/\u Layout.cshtml

@inherits MyApp.CustomViewPage
<!DOCTYPE html>
<html>
...
@if(this.Helper.HasFoo()){@Html.ActionLink("Bar")}
@继承MyApp.CustomViewPage
...
@if(this.Helper.HasFoo()){@Html.ActionLink(“Bar”)}
全球注册

builder.RegisterType<Helper>().AsImplementedInterfaces();
builder.RegisterModelBinderProvider();
builder.RegisterFilterProvider();
builder.RegisterModule(new AutofacWebTypesModule());
builder.RegisterSource(new ViewRegistrationSource());
var container = builder.Build();
DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
builder.RegisterType();
RegisterModelBinderProvider();
builder.RegisterFilterProvider();
RegisterModule(新的AutofacWebTypesModule());
RegisterSource(新的ViewRegistrationSource());
var container=builder.Build();
SetResolver(新的AutofacDependencyResolver(容器));

使用布局的“子”视图不是从CustomViewPage派生的。

不仅仅是使用AutoFac,基本上您无法在布局中实现DI。您可能需要在
CustomViewPage
中引用IOC容器来解决依赖关系

除非它是REALYYY必需的只需在视图中避免DI(只是我的意见)


从我的观点来看,我看不出有什么好处。我认为您不会为基本视图页面类编写单元测试,是吗?除非有什么特别的原因,尽量避免。与其依赖于容器,不如依赖于具体的实现。

这里有一个小的解决方法,可以用于大多数DI框架

首先稍微调整一下CustomPageView:

public abstract class CustomViewPage : WebViewPage
{
    public IHelper Helper { 
        get { return ViewData[Helper.ViewDataKey] as IHelper; }    
    }

}
现在我们需要将依赖性引入到ViewData中,引入一个属性来实现这一点:

public sealed class HelperAttribute : ActionFilterAttribute
{
    public override void OnResultExecuting(ResultExecutingContext filterContext)
    {
        var viewResult = filterContext.Result as ViewResult;
        if (viewResult != null)
            viewResult.ViewData.Add(Helper.ViewDataKey, GetHelperFromIoC());

        base.OnResultExecuting(filterContext);
    }
}
在您的操作方法或控制器上:

[Helper]
public ActionResult Index()
{
    return View();
}
在您看来,您现在应该能够按预期使用您的助手:

 @Helper.HelloWorld()

请参阅原始帖子。

大多数解决方案都只是DependencyResolver.Current.GetService调用的包装,因此直接从布局调用它可能更容易:

@{
    var helper = DependencyResolver.Current.GetService<IHelper>();
}
...
@if (helper.HasFoo()) { @Html.ActionLink("Bar") }
...
@{
var helper=DependencyResolver.Current.GetService();
}
...
@if(helper.HasFoo()){@Html.ActionLink(“Bar”)}
...

另外,这种方式有助于使页面模型更具SRP,因为它可以避免混合服务例程/模型和业务模型。

对于没有参数的结果,您不需要扩展WebViewPage来向其传递数据。 我会这样解决: 1.声明一个从ActionFilter派生的类HelperActionFilter,通过属性向其注入服务
2.Inside HelperActionFilter.OnActionExecuting setup ViewBag.HasFoo在布局中检查它。

只需创建部分页面并将其插入布局页面:

@Html.Partial("_MyPartialPage");

依赖项被注入到部分页面。

这似乎是Autofac的一个问题……是的,这里的问题是这个答案对我来说是一个解决办法:任何关于为什么你不能进入布局的参考或背景?@Josh如上所述,这是我和其他许多人的观点,我从来没有说过你不能,只是这可能不是最好的想法。。。不这样做的几个原因是,很难进行单元测试,这取决于您使用的IoC库,您需要访问HttpContext来解析容器,并且您可能最终会遇到视图直接与服务对话的情况,从而破坏MVC模式。这取决于每个人如何做事,我不是说你不能只是想重新思考设计,确保没有更好的option@Joe_DM能否将您的编辑作为新答案发布?@VJAI很抱歉,编辑的更改似乎已丢失,不确定原因:(如果有什么方法可以在历史中找到它们,我很乐意,否则从头开始重新创建就太麻烦了。@VJAI我知道发生了什么,我编辑了别人的答案,我认为这是我的答案:O如果有什么方法可以找回我在答案中输入的代码,我很乐意作为新答案发布。