csharp Asp.Net过滤注入

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了csharp Asp.Net过滤注入相关的知识,希望对你有一定的参考价值。

public class WebApiUnityFilterProvider : ActionDescriptorFilterProvider, IFilterProvider
    {
        private readonly IUnityContainer container;

        public WebApiUnityFilterProvider(IUnityContainer container)
        {
            this.container = container;
        }

        public new IEnumerable<FilterInfo> GetFilters(HttpConfiguration configuration, HttpActionDescriptor actionDescriptor)
        {
            var filters = base.GetFilters(configuration, actionDescriptor);
            List<FilterInfo> filterInfoList = new List<FilterInfo>();

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

            return filters;
        }
    }
  internal class ActionFilterWrapperAttribute : FilterAttribute, IActionFilter
    {
        private readonly Type filter;

        public ActionFilterWrapperAttribute(Type filter)
        {
            this.filter = filter;
        }

        public void OnActionExecuted(ActionExecutedContext filterContext)
        {
            IActionFilter filterInstance = (IActionFilter)DependencyResolver.Current.GetService(filter);

            filterInstance.OnActionExecuted(filterContext);
        }

        public void OnActionExecuting(ActionExecutingContext filterContext)
        {
            IActionFilter filterInstance = (IActionFilter)DependencyResolver.Current.GetService(filter);

            filterInstance.OnActionExecuting(filterContext);
        }
    }
container.RegisterGlobalActionFilter<MvcTestFilterAttribute>();
GlobalFilters.Filters.Add(new ActionFilterWrapperAttribute(typeof(MvcTestFilterAttribute)));

[ActionFilterWrapper(typeof(MvcTestFilterAttribute))]
//
// GET: /Mvc2/
public ActionResult Index()
{
    return View();
}
public static class UnityWebActivator
    {
        /// <summary>Integrates Unity when the application starts.</summary>
        public static void Start() 
        {
            var container = UnityConfig.GetConfiguredContainer();

            FilterProviders.Providers.Remove(FilterProviders.Providers.OfType<FilterAttributeFilterProvider>().First());
            FilterProviders.Providers.Add(new UnityFilterAttributeFilterProvider(container));
            
            DependencyResolver.SetResolver(new UnityDependencyResolver(container));

            Microsoft.Web.Infrastructure.DynamicModuleHelper.DynamicModuleUtility.RegisterModule(typeof(UnityPerRequestHttpModule));
        }
    }
public static class UnityContainerMvcExtensions
{
    public static void RegisterGlobalActionFilter<T>(this IUnityContainer container)
        where T : IActionFilter
    {
        container.RegisterType<T>(new PerRequestLifetimeManager());

        GlobalFilters.Filters.Add(new ActionFilterWrapperAttribute(typeof(T)));
    }
}
GlobalFilters.Add(new SimpleMvcFilterAttribute());
GlobalFilters.Add(new FilterWithCollaborators(new LoggingCollaborator(), new UnitofWork()));
 public static class UnityContainerWebApiExtensions
    {
        public static void RegisterGlobalActionFilter<T>(this IUnityContainer container)
            where T : IActionFilter
        {
            container.RegisterType<T>(new PerRequestLifetimeManager());

            GlobalConfiguration.Configuration.Filters.Add(new ActionFilterWrapperAttribute(typeof(T)));
        }
    }
 public static class UnityWebApiActivator
    {
        /// <summary>Integrates Unity when the application starts.</summary>
        public static void Start() 
        {
            var resolver = new UnityHierarchicalDependencyResolver(UnityConfig.GetConfiguredContainer());

            var defaultProviders = GlobalConfiguration.Configuration.Services.GetFilterProviders();
            GlobalConfiguration.Configuration.Services.Remove(typeof(IFilterProvider), defaultProviders.First(f => f is ActionDescriptorFilterProvider));
            GlobalConfiguration.Configuration.Services.Add(typeof(IFilterProvider), new WebApiUnityFilterProvider(UnityConfig.GetConfiguredContainer()));

            GlobalConfiguration.Configuration.DependencyResolver = resolver;
        }
    }

以上是关于csharp Asp.Net过滤注入的主要内容,如果未能解决你的问题,请参考以下文章

csharp asp.net mvc控制器使用依赖注入

csharp ASP.NET Core-获取所有注入(DI)服务

Asp.Net MVC学习总结之过滤器详解(转载)

csharp 在asp.net webforms中注入服务器端的客户端脚本

csharp 过滤属性以捕获和记录ASP.NET中的请求和响应

ASP.NET 实现SQL注入过滤