actionfilter

How can I get the actionName in a ActionFilter?

this is my old code protected override bool OnPreAction(string actionName, System.Reflection.MethodInfo methodInfo) { if ("|Register|RegisterPage|Login|LoginPage|Logout|Service".ToLower().Contains(actionName.ToLower())) { return base.OnPreAction(actionName, methodInfo); } Customer =...

ASP.NET MVC: How to create an action filter to output JSON?

My second day with ASP.NET MVC and my first request for code on SO (yep, taking a short cut). I am looking for a way to create a filter that intercepts the current output from an Action and instead outputs JSON (I know of alternate approaches but this is to help me understand filters). I want to ignore any views associated with the acti...

In ASP.NET MVC, how to define in which sequence my custom attributes are checked/applied?

I'm currently investigating the possibility to use custom attributes derived from ActionFilterAttribute. I want to accomplish a couple of things with a couple of attributes. The thing is I want to make sure one of the attributes comes into play first, and not in any random sequence. Example: public class Feature1Attrubute : ActionFilte...

ASP.NET MVC, ActionFilters, static classes and passing data around...

I'd like to hear your opinions and maybe better suggestions for the following scenario: I have define a custom ActionFilter that does some job and comes out with some value. I would like to use that value in controller actions and in models. Now, I could use TempData to pass this value from the ActionFilter to any controller action met...

Which redirect has precedence - the one in controller action or in ActionFilter's OnActionExecuted?

Looks like I'm about to do something weird again... The situation: public ExperimentAttribute { public override void OnActionExecuted (ActionExecutingContext filterContext) { filterContext.Result = new RedirectToRouteResult ( new RouteValueDictionary (new { Action = "Action2",...

TranslateAttribute for my asp.net-mvc site

In my current project I have a custom ViewData that has (amongst others) the following properties: CustomViewData + IList <Language> siteLangauges + Language CurrentLanguage + T9nProvider All my URL's go like this: http://someUrl.com/{siteLanguage}/{restOfUrlIncludingcontrollersAndACtions} I want to create an ActionAttribute ...

Calling FilterAttribute's OnActionExecuting before BaseController's OnActionExecuting

I have a BaseController in which I put in some data in the ViewData collection by overriding OnActionExecuting. Now i have an Action in a ChildController that doesn't need that view data. For that purpose I created an DontPopulateViewData ActionFilterAttribute that sets a bool on the BaseController that prevents the BaseController from...

What methods are available to stop multiple postbacks of a form in ASP.NET MVC?

A common web problem is where a user clicks the submit button of a form multiple times so the server processes the form more than once. This can also happen when a user hits the back button having submitted a form and so it gets processed again. What is the best way of stopping this from happening in ASP.NET MVC? Possibilities as I se...

How do I access the ModelState from an ActionFilter?

I'm building an ActionFilter to reuse some code for a simple spam block - basically what I do is that I have a Html Helper method that renders an input textbox and a hidden input, and in the ActionFilter I check whether the two values are the same or not. If not, I want to leverage the rest of my validation logic and add a ModelStateErro...

Why do none of my ActionFilters run?

I asked a question earlier today about ActionFilters in ASP.Net MVC. It turned out my problem was really that my ActionFilter is not even running. Among other things I read this article, and I can't find anything he does that I don't. This is my code: // The ActionFilter itself public class TestingIfItWorksAttribute : ActionFilterAttri...

How do I UnitTest a custom ActionFilter?

I've been trying to find some straightforward information on this, but I haven't been able to - either what I've found has been to vague for me to understand what's going on, or too specific for the wrong thing, for example a tutorial I found for unit testing an AuthorizeAttribute. (A third alternative is of course that I'm too dumb to r...

Check User.Identity.IsAuthenticated in ActionFilter right after login

I'm basically using the AccountController from the ASP.NET MVC samples. It uses FormsAuthentication to handle the user login. Just to be sure, here is the code to handle user login: public ActionResult LogOn(string userName, string password, bool rememberMe, string returnUrl) { if (!ValidateLogOn(userName, password)) ...

ActionFilter to set a controller.action parameter but param req on ActionLink in View

We have a [UserNameFilter] ActionFilterAtrribute which populates the controller actions username parameter. This works nicely. However, when I use the Html.ActionLink helper stringly typed to the controller, the compiler requests said username parameter in the View. Has anyone come across this and know how to recitify? Controller Actio...

asp.net mvc - Route for string or int (i.e. /type/23 or /type/hats)

Hi guys I have the following case where I want to accept the following routs '/type/view/23' or '/type/view/hats' where 23 is the Id for hats. The controller looks something like this: public class TypeController { [AcceptVerbs(HttpVerbs.Get)] public ActionResult View(int id) { ... } } Now if they pass in...

ASP.NET MVC: Not executing actionfilters on redirect and throw HttpException

Hi, I've created an OnActionExecuted filter to populate some viewmodel attributes with data from db (I'm not using ViewData["somekey"], preferring many ViewModels descending from a common ancestor). public class BaseController : Controller { protected DataClassesDataContext context = new DataClassesDataContext(); protected ove...

Prevent Caching of Attributes in ASP.NET MVC, force Attribute Execution every time an Action is Executed

According to various articles (e.g. here and here) attribute results on ASP.NET MVC Actions may be cached and not executed again when a controller action is called. That behavior is not desired in my case (e.g. I have an authorization system based on my own attributes and IPs, role checks that need to execute every time, and other thing...

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

I have written an action filter which detects a new session and attempts to redirect the user to a page informing them that this has happened. The only problem is I can not figure out how to make it redirect to a controller/action combo in an action filter. I can instead only figure out how to redirect to a specified url. Is there a d...

.NET MVC Write Cookie from ActionFilter

Hi, I built an ActionFilter to host a page hit logger on my MVC site and have the need to save some values into cookie. I've used the following code to write the cookie: public class LogRequestAttribute : ActionFilterAttribute, IActionFilter { void IActionFilter.OnActionExecuting(ActionExecutingContext filterContext) { if(...

Inject referrer action via action filter?

Is there a way to inject the referrer action from an action filter? Lets say I have a view that comes from action X. In dies view I call action Y and I want to redirect again to action X. (There are multiple X actions that call action Y). I thought that it could be nice if I had a parameter call referrerAction and an action filter that ...

Using Spring.Net to inject dependencies into ASP.NET MVC ActionFilters

I'm using MvcContrib to do my Spring.Net ASP.Net MVC controller dependency injection. My dependencies are not being injected into my CustomAttribute action filter. How to I get my dependencies into it? Say you have an ActionFilter that looks like so: public class CustomAttribute : ActionFilterAttribute, ICustomAttribute { private IA...