How do I unit test a custom ActionFilter in ASP.Net MVC

You just need to test the filter itself. Just create an instance and call the OnActionExecuted() method with test data then check the result. It helps to pull the code apart as much as possible. Most of the heavy lifting is done inside the CsvResult class which can be tested individually. You don’t need to … Read more

How do I add a parameter to an action filter in asp.net?

The TypeFilterAttribute has an Argument property (of type object[]) where you can pass arguments to the constructor of the implementation. So applied to your example you can use this code: public class MyAttribute : TypeFilterAttribute { public MyAttribute(params string[] ids) : base(typeof(MyAttributeImpl)) { Arguments = new object[] { ids }; } private class MyAttributeImpl : … Read more

How to disable a global filter in ASP.Net MVC selectively

You could write a marker attribute: public class SkipMyGlobalActionFilterAttribute : Attribute { } and then in your global action filter test for the presence of this marker on the action: public class MyGlobalActionFilter : ActionFilterAttribute { public override void OnActionExecuting(ActionExecutingContext filterContext) { if (filterContext.ActionDescriptor.GetCustomAttributes(typeof(SkipMyGlobalActionFilterAttribute), false).Any()) { return; } // here do whatever you were intending … Read more

Injecting dependencies into ASP.NET MVC 3 action filters. What’s wrong with this approach?

Yes, there are downsides, as there are lots of issues with IDependencyResolver itself, and to those you can add the use of a Singleton Service Locator, as well as Bastard Injection. A better option is to implement the filter as a normal class into which you can inject whichever services you’d like: public class MyActionFilter … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)