StructureMap自定义ActionFilter

时间:2016-11-03 08:41:51

标签: c# asp.net asp.net-mvc structuremap

我使用的是Asp.Net MVC。我使用结构图定制了AcitonFilter。

在自定义ActionFilter中,我使用枚举类型。

public enum TimeUnit
{
    Minute = 60,
    Hour = 3600,
    Day = 86400
}

我的ActionFilter类是:

[AttributeUsage(AttributeTargets.Method, AllowMultiple = true)]
public class CustomAttribute : ActionFilterAttribute
{
    public TimeUnit Time { get; set; }

    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        var seconds = Convert.ToInt32(Time);

    }
}

我的ActionFilter的结构图设置是:

public class StructurMapFilterProvider : FilterAttributeFilterProvider
{
    private readonly Func<StructureMap.IContainer> _Container;

    public StructurMapFilterProvider(Func<StructureMap.IContainer> container)
    {
        _Container = container;
    }

    public override IEnumerable<Filter> GetFilters(ControllerContext controllerContext,
        ActionDescriptor actionDescriptor)
    {
        var filters = base.GetFilters(controllerContext, actionDescriptor);

        var container = _Container();

       foreach (var filter in filters)
        {
            container.BuildUp(filter.Instance);

            yield return filter;
        }
    }
}

在我使用ActionFilter后,在

中抛出异常
  

container.BuildUp(filter.Instance);

异常消息是:

  

Setter Property&#39; Time&#39; (ActionFilters.TimeUnit):未明确定义必需的原始依赖

我这样用过:

[Custom(Time = TimeUnit.Hour)]
    public ActionResult MyAction(ViewModel model)
    {
        // my code
    }

我该怎么办? 如何明确定义枚举?

0 个答案:

没有答案