actionfilter

Is it OK to send a 301 redirect with an action filter?

I'm writing a new asp.net mvc application and I've been toying with the idea of allowing my user to post short, concise urls to content that he has posted. Those short url's will be handy in cramped spaces like Twitter and comment areas. I like this idea as I'm not a huge fan of url shorteners because they're so vague and you're never re...

Flushing and Compression filters (ASP.NET MVC)

We have quite common code which worked fine: public class CompressionFilterAttribute : ActionFilterAttribute { public override void OnActionExecuting(ActionExecutingContext filterContext) { HttpRequestBase request = filterContext.HttpContext.Request; if (request.IsAjaxRequest(...

ASP.NET MVC ActionFilter parameter binding

If you have a model-bound parameter in an action method, how can you get to that parameter in an action filter? [MyActionFilter] public ActionResult Edit(Car myCar) { ... } public class MyActionFilterAttribute : ActionFilterAttribute { public void OnActionExecuted(ActionExecutedContext filterContext) { //I want to a...

Action Filter ActionParameters

I have an ActionFilterAttribute which I want to accept parameters through but I can't figure out pass them across. So my action filter looks like this; public class PreventAction : ActionFilterAttribute { public override void OnActionExecuting(ActionExecutingContext filterContext) { filterContext.Result = new RedirectRe...

Changing ActionExecutingContext values in Custom Filter Attribute

I have a custom filter attribute that I want to use on certain ActionResults to look at the data being attempted and to set values where certain rules are violated. So; public class SpecialActionFilter : ActionFilterAttribute { public override void OnActionExecuting(ActionExecutingContext filterContext) { foreach (strin...

Catching ASP.Net MVC Principle Permission attribute exceptions

Hi, I would like to secure my MVC controller actions using... [PrincipalPermission(SecurityAction.Demand, Role="Administrator")] However, if the user is not in this role then a SecurityException "Request for principal permission failed." is thrown by the code. There seems to be no way to handle this error, even [Handle] error wont ca...

Modify a model in an ActionFilter

Hello, I have an actionfilter that I am running OnActionExecuting in ASP.NET MVC 2. Essentially I would like the actionfilter to sanitize my data and replace the current model (which will be passed to subsequent action filters and also my action method) with the sanitized model. Is this possible and is it a bad idea - if so why? Thank y...

Testing an MVC Controller action with an ActionFilterAttribute

A well-known benefit of MVC is its suitablility for Test Driven Development (TDD) because you can directly call your controller actions from your test methods. How can you test the combination of a controller action with a ActionFilter attribute (using OnActionExecuted to modify the ActionResult returned by the Action)? If I just call ...

asp.net MVC ActionFilter for remove empty lines in result

Please help me with this action filter. I think i need to use OnResultExecuted method How can i have access to otput html and replace something in them? thank you. ...

Redirect loop with SSL action filter in ASP.NET MVC

I am using an ActionFilter (see below) to detect whether or not 1. the current controller/action requires SSL and 2. SSL is currently being used, and redirect accordingly. This works fine locally (using a dummy cert in IIS 7) but once I get it up on the server I get an error indicating an infinite redirect loop. Any ideas? public cla...

ASP.NET MVC OutputCache JSONP

I cache everything that is possible on an ASP.NET MVC website and it works perfect. Now I have created an API where the calls go to Controller Actions. (http://mysite.com/topics/latest.json) The API is able to return results in different formats (json, xml, rss). The data for returning is loaded in the Action: [ResponseFilter] public ...

Unit testing an ActionFilter - correctly setting up the ActionExecutingContext

In a custom ActionFilter, I want check the attributes on the controller action that will be executed. Running through a small test application, the following works when launching the app in the asp.net development server- public class CustomActionFilterAttribute : ActionFilterAttribute { public override void OnActionExecuting(Action...

Want to create a action filter to force Url to be using SSL

I want to create a action filter that will check the url, and if its not using Ssl, redirect to the same page but with SSL. What is the best way of doing this? Should I just check the RawUrl, and scan the string for https, and if its not there then do: context.Response.Redirect(context.Request.RawUrl.Replace("http:", "https:")); ...

Model availability inside ActionFilter

I have created a new ActionFilter for an ASP.NET MVC application that I'm creating. I have an action which accepts an Http Post and the argument of the action method accepts an object, for which I have created and registered a custom model binder. I noticed that inside the IActionFilter.OnActionExecuting the value for filterContext.Cont...

Trace not working in OnResultExecuted() in MVC

I'm trying to output to Trace.axd from an ASP.NET MVC app. I have traces in OnActionExecuting(), OnActionExecuted(), OnResultExecuting() and OnResultExecuted() and all of them output to Trace.axd except for OnResultExecuted(). For completeness' sake here's my code (exactly the same for OnActionX() ): protected override void OnResu...

Stop continuation of ASP.NET MVC ActionFilter

I have two custom ActionFilters on an action. In first of the actionfilters, I have an redirect performed if a condition is not met (classic authorization). And in another I have an redirect performed if another condition is not met (say role checking). But I do not want to continue to the second actionFilter if the first one is not ...

What is the order of execution in ASP.NET MVC Controller?

Say I have a controller called "HomeController" which inherits from Mvc.Controller. Also say I have written the constructor of the controller and some filters for some actions. Public Class ClientController Inherits System.Web.Mvc.Controller Public Sub New() ''Some code End Sub <SomeActionFilter()> _ Functi...

Using Action Filters for user login in Asp.NET MVC?

I recently built a site using Asp.Net. The way I chose to implement user login is through a base 'UserAwareController' class, that all controllers extend. It contained a reference to the UserRepository, and exposed a protected GetCurrentUser() method that concrete controllers could query. The whole process felt a bit wishy-washy to me....

How do I test ActionFilterAttributes that work with ModelState?

As suggested by (among others) Kazi Manzur Rashid in this blog post, I am using ActionFilterAttributes to transfer model state from one request to another when redirecting. However, I find myself unable to write a unit test that test the behavior of these attributes. As an example, this what I want the test for the ImportModelStateAttri...

How do the httppost, httpput etc attributes in ASP.NET MVC 2 work?

In ASP.NET MVC 2, a couple of new action filter attributes were introduced, as "shorthand" for attributes in ASP.NET MVC 1; for example, applying the HttpPostAttribute does the same thing as applying [AcceptVerbs(HttpVerbs.Post)] to an action method. In addition, with the more verbose syntax, it is possible to combine different methods,...