actionfilter

Response.Redirect not working inside an custom ActionFilter

My code is the following public class SessionCheckAttribute : ActionFilterAttribute { public override void OnActionExecuting(ActionExecutingContext filterContext) { if (/*condition*/) { filterContext.HttpContext.Response.Redirect("http://www.someurl.com",true); } base.OnActionExecuti...

Rails: getting logic to run at end of request, regardless of filter chain aborts?

Is there a reliable mechanism discussed in rails documentation for calling a function at the end of the request, regardless of filter chain aborts? It's not after filters, because after filters don't get called if any prior filter redirected or rendered. For context, I'm trying to put some structured profiling/reporting information int...

Testing ActionFilterAttributes with MSpec

I'm currently trying to grasp MSpec, mainly to learn new ways of (T/B)DD to be able to make an educated decision on which technology to use. Previously, I've mostly (read: only) used the built-in MSTest framework with Moq, so BDD is quite new for me. I'm writing an ASP.NET MVC app, and I want to implement PRG. Last time I did this, I us...

How to intercept 401 from Forms Authentication in ASP.NET MVC?

I would like to generate a 401 page if the user does not have the right permission. The user requests a url and is redirected to the login page (I have deny all anonymous in web.config). The user logs in successfully and is redirected to the original url. However, upon permission check, it is determined that the user does not have the...

What is the order of execution when dealing with .NET MVC 2 Action Filters?

Say I have: [Attribute1(Order=0)] public class Controller1 { [Attribute2] [Attribute3] public ActionResult Action1() { ... } } The attributes get executed in the following order: 2, 3, 1 This makes sense because attributes 2 and 3 have an order of -1 and will be executed before attribute 1 which has an explici...

How to identify the Actionfilter used in a Controller Action Method?

OnActionExecuting triggers when an action is about to execute. If my action has actionfilter [myCustomActionFilter] public ActionResult MyAction() { //implementation } Is it possible to determine (inside the OnActionExecuting event) that an action has myCustomActionFilter applied into it? ...

repeat common error info logic in ActionFilterAttribute

I'm implementing a web API using the REST for ASP.NET MVC framework (MVC 2). I want to encapsulate this code, ideally in an ActionFilterAttribute (?), so that I can decorate specific actions that always perform this same logic: if (!ModelState.IsValid) { return View( new GenericResultModel(){ HasError=True, ErrorMessage="Model is ...

Authorize filters vs Action Filters

Hello, i m using .NET mvc2 for my application. i want some custom authorization on my actions. i have googled a bit and there seems to be two options available. Impelement logic in onActionExecuting in custom Action Filter see this post subclass authorizeattribute or implement Iauthorization interface and put my logic there My quest...

Call controller action in current context from, for example, Filter

I've tried to call controller action from filter. But i've discovered that if i want to do that i had to.. var controller = new MyController(); controller.ControllerContext = filterContext.Controller.ControllerContext; controller.<action>(<parameters>); // it's action which accepts only POST, but here it doesn't matter base.OnActionExec...

How to dynamically determine which HTML 'wrapper' file to use based on the Action Method being executed?

We have an MVC app that injects our content into a HTML wrapper which supplied by the client. There are multiple wrappers, such as fund.html, share.html, factsheet.html, and home.html The wrapper is stored on our webserver. We need to load the wrapper into Site.Master and inject our content into the wrapper's content area, and then r...

Can an Action Filter have access to a private object in the Controller?

I have public class FundController { private Site _site; public ViewResult Fund() { } } I'd like to add an Action Filter to this Fund method: public class FundController { private Site _site; [MyFilter] public ViewResult Fund() { } } but the Action Filter needs access to _site. Is this possib...

ASP.NET ActionFilters and inheritance.

All my controllers inherit from a BaseController that has an ActionFilter attribute: [AnalyticsData] public class BaseController : Controller {} public class AccountController : BaseController {} Some of my Actions in my controllers reuse the AnalyticsData ActionFilter: public class AccountController : BaseController { [Analytic...

How to make an MVC AttributeFilter to verify posted file extension

I have a controller that handles file uploads. Ultimately I would like to be able to create attribute to decorate my controller actions like [HttpPostedFileType("zip")] or something similar. Currently I created this extension method which I use in the action. public static string GetFileExtension(this HttpPostedFileBase file) { ...

ASP.NEt MVC 3 P1 Dependency Injection to Filters

I have read through Brad Wilson's series of "ASP.NET MVC 3 Service Location" (http://bradwilson.typepad.com/blog/2010/07/service-location-pt4-filters.html) and tried to get the DI work with Unity, but it would not (System.NullReferenceException). Have searched everywhere but can find nothing that takes up the subject. So, how do I resolv...

Are there any action filters in your project you feel are a must-have?

I'm still not totally clear on why I would need to build custom action filters. Maybe a couple examples would help. Are there any action filters in your project that you feel are a must-have? Maybe even so important that you re-use them across all your MVC projects? ...

ASP.NET MVC ActionFilter

Does anybody knows if the OnResultExecuted method from ActionFilterAttribute class is executed even in CATCH block? ie: [CookiesActions] public ActionResult Login(Usuarios usuario) [...] return View(new UsersViewModel(sceUsuarios.Usuario,true)); } catch { r...

Is it possible to make data from an Action Method available in an Action Filter?

The Background: We are supplied with html files - 'wrappers' - from our client, into which we need to inject the content that we produce. They have different wrappers for different pages and we have to inject the corresponding content into a special tag that they supply in the wrapper. The wrapper file name corresponds to the name of t...

Unit testing all controllers from a single test

I just created an action filter that I want to apply to nearly all of my controllers (including any new ones that are introduced later on). I figure a really useful unit test would be one that cycles through each controller and verifies that if certain criteria are met, then the action filter will impact the result. Is it wise to creat...

MVC using Action Filter to check for parameters in URL. stop action from executing

I want to make the following: when the url doesn't have an instID, i want to redirect to the "Instelling" action in this controller, every method needs the instID. [RequiredParameter(parameterName="instID", controllerToSend="Instelling")] public ActionResult Index(int? instID) { //if (!instID.HasValue) { ...

How to automatically overload DELETE and PUT if they are not available by the client?

How can I detect at the startup of the application that a client doesn't support DELETE and PUT verbs and automatically overload the POST verb? On the server side, how can I redirect those overloaded POST verbs into the right actions? Say I have a DELETE request that is overriden, how do I call the appropriate function in the controller ...