Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC event that happens just before action is called?

Tags:

asp.net-mvc

I want to set the value of Thread.CurrentCulture based on some route data, but I can't find an event to hook to that fires after the routes are calculated and before the action method is called.

Any ideas?

like image 547
Rei Miyasaka Avatar asked Nov 05 '10 07:11

Rei Miyasaka


People also ask

What are action methods in MVC?

ASP.NET MVC Action Methods are responsible to execute requests and generate responses to it. By default, it generates a response in the form of ActionResult. Actions typically have a one-to-one mapping with user interactions.

What is action result in ASP.NET MVC?

Action Result is actually a data type. When it is used with action method, it is called return type. As you know, an action is referred to as a method of the controller, the Action Result is the result of action when it executes. In fact, Action Result is a return type.

What is action method in asp net core MVC?

An action (or action method) is a method on a controller which handles requests. Controllers logically group similar actions together. This aggregation of actions allows common sets of rules, such as routing, caching, and authorization, to be applied collectively. Requests are mapped to actions through routing.


1 Answers

You could write a custom action filter attribute:

public class CustomFilterAttribute : ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        // This method is executed before calling the action
        // and here you have access to the route data:
        var foo = filterContext.RouteData.Values["foo"];
        // TODO: use the foo route value to perform some action

        base.OnActionExecuting(filterContext);
    }
}

And then you could decorate your base controller with this custom attribute. And here's a blog post illustrating a sample implementation of such filter.

like image 90
Darin Dimitrov Avatar answered Nov 15 '22 10:11

Darin Dimitrov