Register global filters in ASP.Net MVC 4 and Autofac

There’s a new way of registering MVC global filters in AutoFac. First, remove the filter registration from your RegisterGlobalFilters because we will have Autofac handle adding them to our controllers/actions instead of MVC. Then, register your container as follows: var builder = new ContainerBuilder(); builder.RegisterControllers(Assembly.GetExecutingAssembly()); builder.RegisterType<MyProperty>().As<IProperty>(); builder.Register(c => new CustomFilterAttribute(c.Resolve<IProperty>())) .AsActionFilterFor<Controller>().InstancePerHttpRequest(); builder.RegisterFilterProvider(); IContainer container = … Read more

Propagating QueryString parameter in RedirectToAction calls

public class PreserveQueryStringAttribute : ActionFilterAttribute { public override void OnActionExecuted(ActionExecutedContext filterContext) { var redirectResult = filterContext.Result as RedirectToRouteResult; if (redirectResult == null) { return; } var query = filterContext.HttpContext.Request.QueryString; // Remark: here you could decide if you want to propagate all // query string values or a particular one. In my example I am // … Read more

How to use Action Filters with Dependency Injection in ASP.NET CORE?

If you want to avoid the Service Locator pattern you can use DI by constructor injection with a TypeFilter. In your controller use [TypeFilter(typeof(MyActionFilterAttribute), Arguments = new object[] {10})] public IActionResult() NiceAction { … } And your ActionFilterAttribute does not need to access a service provider instance anymore. public class MyActionFilterAttribute : ActionFilterAttribute { public … Read more

Best way to abort/cancel action and response from ActionFilter

Setting the response will mean the action doesn’t get called. public override void OnActionExecuting(HttpActionContext actionContext) { actionContext.Response = new HttpResponseMessage(HttpStatusCode.Unauthorized); } As other answers have said, though, authentication should be done with an AuthorizeAttribute (Docs for Web.API or for MVC).

Get ActionName, ControllerName and AreaName and pass it in ActionFilter Attribute

You could fetch them from the RouteData: protected override bool AuthorizeCore(System.Web.HttpContextBase httpContext) { var rd = httpContext.Request.RequestContext.RouteData; string currentAction = rd.GetRequiredString(“action”); string currentController = rd.GetRequiredString(“controller”); string currentArea = rd.Values[“area”] as string; … }

Get User Name on Action Filter

Bit late for an answer but this is best solution if you are using HttpActionContext in your filter You can always use it as mentioned here:- public override Task OnActionExecutingAsync(HttpActionContext actionContext, CancellationToken cancellationToken) { if (actionContext.RequestContext.Principal.Identity.IsAuthenticated) { var userName = actionContext.RequestContext.Principal.Identity.Name; } }

How to pass parameters to a custom ActionFilter in ASP.NET MVC 2?

This is a way to make this work. You have access to the ControllerContext and therefore Controller from the ActionFilter object. All you need to do is cast your controller to the type and you can access any public members. Given this controller: public GenesisController : Controller { [CheckLoggedIn()] public ActionResult Home(MemberData md) { return … Read more

Best place to set CurrentCulture for multilingual ASP.NET MVC web applications

I used a global ActionFilter for this, but recently I realized, that setting the current culture in the OnActionExecuting method is too late in some cases. For example, when model after POST request comes to the controller, ASP.NET MVC creates a metadata for model. It occurs before any actions get executed. As a result, DisplayName … Read more

Why call base.OnActionExecuting(filterContext);?

Should I be always be calling the base methods after? That will depend on the situation. For example in authorization filters (deriving from AuthorizeAttribute) if you call the base method then all the existing authorization logic built into ASP.NET MVC will be executed. If you don’t call it, only your authorization logic will be applied. … Read more

Redirecting to specified controller and action in asp.net mvc action filter

Rather than getting a reference to HttpContent and redirecting directly in the ActionFilter you can set the Result of the filter context to be a RedirectToRouteResult. It’s a bit cleaner and better for testing. Like this: public override void OnActionExecuting(ActionExecutingContext filterContext) { if(something) { filterContext.Result = new RedirectToRouteResult( new RouteValueDictionary {{ “Controller”, “YourController” }, { … Read more