Questions tagged [custom-action-filter]

In ASP.NET MVC, an Action Filter consists of logic that runs directly before or directly after a Controller action method runs. Custom Action Filters can be used for logging, authentication, output caching, or other tasks.

ASP.NET MVC supports the following types of action filters:

  • Authorization filters
  • Action filters
  • Result filters
  • Exception filters
82 questions
42
votes
5 answers

Asp.net mvc - Accessing view Model from a custom Action filter

I am trying to access the Model data passed to the view in the action filter OnActionExecuted. Does anyone know if this is possible? I am trying to do something like this: public override void OnActionExecuted(ActionExecutedContext filterContext) { …
ebrown
  • 801
  • 1
  • 8
  • 13
28
votes
1 answer

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

I have the following filter attribute, and i can pass an array of strings to the attribute like this [MyAttribute("string1", "string2")]. public class MyAttribute : TypeFilterAttribute { private readonly string[] _ids; public…
7
votes
3 answers

ASP.NET MVC - CustomeAuthorize filter action using an external website for loggin in the user

I have a CustomeAuthorize action filter that forwards the user to signin page if user is not authenticated. I apply this filter to actions or controllers. [CustumeAuthorize] public ActionResult MyAction() { //do something here return…
xraminx
  • 1,126
  • 4
  • 13
  • 19
7
votes
2 answers

ASP.NET Core Action Filter Doesn't Get Called

I have an ASP.NET Core API (.Net Core 2.1) and I implemented an Action Filter using this article https://docs.microsoft.com/en-us/aspnet/core/mvc/controllers/filters?view=aspnetcore-2.1#action-filters In my Model, I use Data Annotations to validate…
5
votes
3 answers

Asp.Net MVC 5 Custom Action Filter With StructureMap

i am facing issue in asp.net mvc custom acitonfilte using structuremap in my "LogAttribute" class i have setter dependency injection which is coming null when executing the "OnActionExecuted" Method of my customfilterclass which is "LogAttribute" my…
Developerzzz
  • 1,041
  • 1
  • 11
  • 23
4
votes
2 answers

same action filter on different action

I'm implementing a custom authorize filter that inherits from AuthorizeAttribute. After my research I found out action filters are cached so they are instantiated only once. Here is my question. If I implement and use a custom action filter like…
3
votes
2 answers

Custom ActionFilterAttribute in .Net Core 3.1 returning: Unable to resolve service for type 'System.String'

I am migrating my .Net Core 2.2 app to version 3.1. I have the following Action Filter: public class MyFilterAttribute : ActionFilterAttribute { private readonly string _name; private readonly int _num; private readonly string _type; …
3
votes
1 answer

ASP.NET MVC 3 Custom Action Filter - How to add incoming model to TempData?

I'm trying to build a custom action filter which grabs the incoming model out of the filter context, adds it to tempdata, then does "other stuff". My action method looks like this: [HttpPost] [MyCustomAttribute] public ActionResult…
RPM1984
  • 69,608
  • 55
  • 212
  • 331
3
votes
0 answers

CallContext value set in web api action filter does not flow to Controller action

I have an asp.net web api where we are starting a transaction using TransactionScope (only for POST,PUT and DELETE but not for GET) in OnActionExecuting of a global action filter and then completing or rolling it back in OnActionExecuted. Recently…
Jags
  • 762
  • 6
  • 17
3
votes
1 answer

Asp.net Core custom filter implementing IActionModelConvention and IFilterFactory

I need to create a custom action filter that implements both IActionModelConvention and IFilterFactory. I use IActionModelConvention for setting several routes at the same time, and I use IFilterFactory to inject some services I need to use. The…
3
votes
2 answers

Show popup from ActionFilter if session expires in mvc

We have created a website using mvc where in I have 3 controllers: AccountController HomeController ContactController We have written custom Authorization in the ActionFilters wherein we are checking whether the session has expired or not. If the…
3
votes
4 answers

How to get MVC Action parameter from AuthorizationContext?

I am currently trying to write a custom authentication filter and I need to access the dto that is being passed as a parameter to the action in my filter. Lets say I have an action like this [AuthenticateProfile] public ActionResult…
Rob Schneider
  • 637
  • 2
  • 13
  • 27
2
votes
1 answer

How to pass dynamic variable to Action Filter in ASP.NET MVC

I would like to use a variable to pass a dynamic value to my action filter. I thought it would be something like this: [MessageActionFilter(message = "User is updating item: " & id)] public ActionResult doSomething(int id) { // do…
Jason
  • 15,436
  • 20
  • 71
  • 112
2
votes
2 answers

Returns from OnActionExecutionAsync without executing the action in asp.net core

Here I want to return from the custom action filter without executing the controller action method in asp.net core WEB API. Below is my requirement with sample code. public override async Task OnActionExecutionAsync(ActionExecutingContext context,…
2
votes
1 answer

How can I pass an instance of LoggerFactory to ActionFilterAttribute

Good day. I'm trying to use logging by injecting a LoggerFactory in my custom ActionFilterAttribute class but when using the Attribute in one of the controller methods I get an error saying [CS7036] There is no argument given that corresponds to the…
Ash
  • 498
  • 6
  • 19
1
2 3 4 5 6