22107

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

Question:

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 override void OnActionExecuted(ActionExecutedContext filterContext) { ViewModel model = (ViewModel) ViewData.Model; model.IsUserAuthenticated = filterContext.HttpContext.User.Identity.IsAuthenticated; if (model.IsUserAuthenticated) { model.UserName = filterContext.HttpContext.User.Identity.Name; } model.CommonAttribute = from c in context.Something select new SomethingElse() {...}; } }

The problem is that when an action results in a redirect or a 404 error, OnActionExecuted tries to access ViewModel, which has not been initialized. Also, it's completely useless to fill those values, as they will not be used, since another action is going to be called.

How can I avoid filling viewodel on redirect?

Answer1:

A trivial solution would be to not fill in the model when it doesn't exist:

ViewModel model = ViewData.Model as ViewModel; if (model != null) { model.IsUserAuthenticated = filterContext.HttpContext.User.Identity.IsAuthenticated; if (model.IsUserAuthenticated) { model.UserName = filterContext.HttpContext.User.Identity.Name; } model.CommonAttribute = from c in context.Something select new SomethingElse() {...}; }

Recommend

  • Overriding Default FOSUserBundle Controller in Symfony 3.4
  • ASP.NET MVC - Custom IIdentity or IPrincipal with Windows Authentication
  • Laravel 4.2 - ReflectionException (-1)
  • ASP.NET MVC : How to create own HttpContext
  • Laravel routing: how to pass parameter/arguments from one controller to controller
  • PHP - Use database in MVC view layer (Laravel Blade)
  • How to find out what changes on a branch after merges from master?
  • XSLT Call-Template name attribute
  • how to count all global variables in the cpp file
  • PHP Equivalent to Ruby's @instance_variable?
  • How to extract element-path from XMLType Node?
  • How to get listview position?
  • Autofac with Web API 2 - Parameter-less constructor error
  • Laravel: Getting Session ID oddly truncates when using foreach
  • ASP.NET MVC Application won't update some controllers
  • Bypass multiple inheritance in Java
  • CakePHP ACL tutorial initDB function warnings
  • Sencha Touch 2.0 Controller refs attribute not working?
  • Recording logins for password protected directories
  • how to do an event when i swipe from fragment to the other
  • Align navbar back button on right side
  • How to add date and time under each post in guestbook in google app engine
  • How to set/get protobuf's extension field in Go?
  • DirectX11 ClearRenderTargetViewback with transparent buffer?
  • Validaiting emails with Net.Mail MailAddress
  • sending/ receiving email in Java
  • Web-crawler for facebook in python
  • Akka Routing: Reply's send to router ends up as dead letters
  • bootstrap to use multiple ng-app
  • Cannot Parse HTML Data Using Android / JSOUP
  • Why can't I rebase on to an ancestor of source changesets if on a different branch?
  • JTable with a ScrollPane misbehaving
  • Java static initializers and reflection
  • unknown Exception android
  • Binding checkboxes to object values in AngularJs
  • Observable and ngFor in Angular 2
  • failed to connect to specific WiFi in android programmatically
  • Unable to use reactive element in my shiny app
  • How can I use threading to 'tick' a timer to be accessed by other threads?
  • How to load view controller without button in storyboard?