Questions tagged [onactionexecuting]

ASP.NET MVC Controller.OnActionExecuting Method. Called before the action method is invoked.

ASP.NET MVC Controller.OnActionExecuting Method.

Called before the action method is invoked.

Remarks: If this method is overridden in a derived Controller class, it will be called for every action method in the class. For more flexibility, derive a class from ActionFilterAttribute and override this method in the derived ActionFilterAttribute class.

42 questions
204
votes
4 answers

How to redirect from OnActionExecuting in Base Controller?

I have tried two ways: Response.Redirect() which does nothing, as well as calling a new method inside of the Base Controller that returns an ActionResult and have it return RedirectToAction()... neither of these work. How can I do a redirect from…
MetaGuru
  • 42,847
  • 67
  • 188
  • 294
5
votes
3 answers

Default parameter in all action before action executing in mvc

Is there a way to add some default parameter to every action in MVC application. So I can get the value of the parameter if send from anchor href. public override void OnActionExecuting(ActionExecutingContext filterContext) { int…
danish farhaj
  • 1,316
  • 10
  • 20
4
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,…
4
votes
2 answers

Url redirection on OnActionExecuting method

We are trying to implement a non-hosted header that will accept anything before *.website.com in ASP.NET. Since it will accept any subdomain, we extended the HttpContextBase class to add custom method. public static bool ValidateHost(this…
rpmansion
  • 1,964
  • 1
  • 9
  • 26
3
votes
2 answers

ASP.NET MVC controller actions with custom parameter conversion?

I want to set up a ASP.NET MVC route that looks like: routes.MapRoute( "Default", // Route name "{controller}/{action}/{idl}", // URL with parameters new { controller = "Home", action = "Index", idl = UrlParameter.Optional } // Parameter…
Factor Mystic
  • 26,279
  • 16
  • 79
  • 95
3
votes
1 answer

mvc logging: [LogRequest] vs OnActionExecuting

i'm a .net/c# noob (long-time servlet/java developer) i need to add logging to my mvc application. i want it to be fairly cheap performance-wise, and simple to configure. what i would initially like to do is log each incoming controller action. it…
horace
  • 938
  • 9
  • 20
3
votes
1 answer

Get expected Action Parameter type in OnActionExecuting

Question: Is it possible to know the type of parameter that is expected by the action being called? For example, I have some action as: [TestCustomAttr] public ActionResult TestAction(int a, string b) { ... and TestCustomAttr is defined…
Zeeshan
  • 2,884
  • 3
  • 28
  • 47
3
votes
1 answer

Controller.OnActionExecuting not firing after first visit to action

In my MVC 4 web app, I have a requirement to ask users to choose a payment method after they have been using the app for a certain number of days. I've implemented this by making a controller base class, and making all my controllers inherit from…
colonel32
  • 43
  • 6
2
votes
1 answer

MVC Get ActionFilterAttribute value in Base Controller OnActionExecuting

If i set an Attribute on an action in a controller that inherits BaseController, is it possible to get that value in some BaseController function? public class BaseController : Controller { protected override void…
Yogurt The Wise
  • 4,379
  • 4
  • 34
  • 42
2
votes
1 answer

Decompress body request with C# in asp.net mvc already compressed with pako library JS

I have an asp.net mvc app and want to compress body of the request in the view with Pako library (using gzip and deflate encoding) and decompress the body request before arriving to the controller using OnActionExecuting and…
2
votes
1 answer

ASP.NET MVC 3 OnActionExecuting causes infinite loop

I have that overriden OnActionExecuting method (to check before action execute if user is logged in) public class AuthenticationAttribute : ActionFilterAttribute { public override void OnActionExecuting(ActionExecutingContext filterContext) …
Tony
  • 12,405
  • 36
  • 126
  • 226
2
votes
2 answers

Instantiation of a controller in OnActionExecuting (...not throwing a 404) in MVC Azure

The objective is to add a maintenance batch on the same url of the administration of an Azure MVC site. The url should be something like: https://admin.mysite.com/Batch?pass=HKE671 I decided to override OnActionExecuting and to capture the…
Manor
  • 61
  • 8
2
votes
0 answers

Action Filters to only load data for certain action types

I am relatively new to the MVC way of doing things and have run into a bit of a performance issue due to loading a lot of extraneous data in a base controller. I have read a bit about action filters and was wondering if anyone had advice on how to…
spotvader
  • 35
  • 4
1
vote
1 answer

What is the equivalent of Web Forms "Page_Load" in ASP.NET Core 5, so my code will run before any page loading?

Is there a way to execute a code on every page load in ASP.NET Core 5, like there is in Web Forms? In Web Forms I used the Page_Load() event handler, but what is the equivalent in ASP.NET Core 5? So if I call any action in any controller, it will…
1
vote
1 answer

Silently prevent WEB API Method execution

I want to deny entry to certain web methods on the weekends. An action filter seemed like the natural vehicle for that. public class RunMonThruFriAttribute : ActionFilterAttribute { public override void OnActionExecuting(HttpActionContext…
americanslon
  • 4,048
  • 4
  • 32
  • 57
1
2 3