I have an ActionFilter that checks if a parameter in the URL is valid. If it is not valid I have to render a View. I dont want to redirect, because I still need the ActionExecutingContext. Can that be done?
public override void OnActionExecuting(ActionExecutingContext filterContext) { Guid processIdentifier = (Guid)filterContext.RouteData.Values["processIdentifier"]; //if processIdentifier not found render a view with message and some other objects in ViewData filterContext.Controller.ViewData.ModelState.AddModelError("WrongProcessIdentifier", "The process-id you supplied is not valid"); base.OnActionExecuting(filterContext); }
It doesn't matter if you implicitly return the ViewResult with return View(); or explicitly pass the view name to the View method with return View("<ViewName>"); . In both cases, view discovery searches for a matching view file in this order: Views/\[ControllerName]/\[ViewName]. cshtml.
If you want to use RedirectToAction : You could make a public RedirectToAction method on your controller (preferably on its base controller) that simply calls the protected RedirectToAction from System. Web. Mvc.
An action filter is an attribute that you can apply to a controller action -- or an entire controller -- that modifies the way in which the action is executed.
Action Filter is an attribute that you can apply to a controller action or an entire controller. This filter will be called before and after the action starts executing and after the action has executed. Action filters implement the IActionFilter interface that has two methods OnActionExecuting andOnActionExecuted.
HandleErrorAttribute
had what I was looking for.
filterContext.Result = new ViewResult { ViewName = "MessagePage", ViewData = filterContext.Controller.ViewData, TempData = filterContext.Controller.TempData };
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With