action-filter

Prevent a ASP.NET MVC global filter from being applied on Elmah action

柔情痞子 提交于 2019-12-23 07:33:23
问题 I'm using Elmah for logging exceptions on my MVC application using Alex Beletsky's elmah-mvc NuGet package. The application registers some global filters, applied on each action called. Is there a way to prevent some of those filters from being applied when calling the Elmah.Mvc.ElmahController error log page ( foo.com/elmah ) ? A test like below works, of course, but I'm looking for a more elegant way that would not involve modifying the filter (nor the source code from Elmah / Elmah MVC).

Model availability inside ActionFilter

自作多情 提交于 2019-12-23 04:28:42
问题 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.Controller.ViewData.Model is always null despite the fact that it looks like the model binder is always invoked before the action filter OnActionExecuting method.

ASP.NET MVC - Job of Controllers

做~自己de王妃 提交于 2019-12-22 10:54:53
问题 I think I'm beginning to be confused with the job of a controller in MVC. I have a service that exposes five functions: list packages in queue get package delete package accept package deny package My ASP.NET MVC controller depends on this service, and can generally execute a service call on an Action. I'm happy so far. The second part is then building the ViewModel result. If I do this inside of the controller, the controller now has an exploding dependency list -- each action added

WebApi Action filter called twice

谁都会走 提交于 2019-12-21 04:32:21
问题 My WebApi filter method OnActionExecuted is being called twice. My filter (I make it as simple as possible): public class NHibernateActionFilter : ActionFilterAttribute { // [Inject] // public ISessionFactoryProvider sessionFactoryProvider { get; set; } public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext) { var a = 5; var b = a; //new BaseSessionProvider(sessionFactoryProvider).EndContextSession(); } } My setup: protected void Application_Start() {

ASP.NET MVC ActionFilter - Determine if AJAX Request

心不动则不痛 提交于 2019-12-21 03:23:24
问题 I am using an ActionFilter to determine if a user has access to a specific resource such as an Account object (a la Rhino Security) before hitting an action. This is a global filter which redirects to an error page should the authorization value fail I'm using the following code, which works fine for full page requests: filterContext.Controller.TempData["ErrorMessage"] = string.Format("You are not authorized to perform operation: {0}", operation); filterContext.Result = new RedirectResult("~

What is the difference between attributes and filters in MVC

泄露秘密 提交于 2019-12-21 03:19:18
问题 Now can I please get a comparison not just a definition. Example: SomeClassAttribute (or ISomeClassAttribute) VS SomeClassFilter (or ISomeClassFilter) I have a feeling that they can be used the same way but generally speaking "an attribute is applied" and a "filter is the functionality they produce." So I could "add an attribute to a method (or class or whatever) to apply a filter. 回答1: "So I could "add an attribute to a method (or class or whatever) to apply a filter." You've got it in that

How do I get the current Url from within a FilterAttribute?

白昼怎懂夜的黑 提交于 2019-12-20 10:16:41
问题 I am writing an Authorize filter attribute adn I'm having trouble figuring out how to get the current url as a string so I can pass it as a parameter to the LogOn action. The goal is that if a user successfully logs on, they will be redirected to the page they were originally trying to access. public override void OnAuthorization(AuthorizeContext filterContext) { base.OnAuthorization(filterContext); ... my auth code ... bool isAuth ; ... my auth code ... if(!isAuth) { filterContext.Result =

Any way to recover model passed to POST action when inside OnException(ExceptionContext filterContext)?

核能气质少年 提交于 2019-12-19 07:01:10
问题 Situation is this: I can't find a way of getting the viewModel that was passed to the POST action method. [HttpPost] public ActionResult Edit(SomeCoolModel viewModel) { // Some Exception happens here during the action execution... } Inside the overridable OnException for the controller: protected override void OnException(ExceptionContext filterContext) { ... filterContext.Result = new ViewResult { ViewName = filterContext.RouteData.Values["action"].ToString(), TempData = filterContext

MVC 3 Dependency Injection with Ninject 2.2 + Global Action Filter

心已入冬 提交于 2019-12-19 02:45:29
问题 I am trying to use ASP.NET MVC 3 and Ninject 2.2 to inject a logger object into a custom ActionFilterAttribute. I am able to get this to work if I mark each controller with the custom attribute. However I cannot get this to work if I remove the attribute decoration from the controllers and try to use a global action filter. Here is the code: under App_Start - NinjectMVC3.cs using NinjectTest.Abstract; using NinjectTest.Concrete; [assembly: WebActivator.PreApplicationStartMethod(typeof

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

大憨熊 提交于 2019-12-18 17:32:08
问题 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 action and just grab the ViewData["Output"], convert it to JSON and send it out the client. Blanks to fill: TestController.cs: [JSON] public ActionResult Index() {