Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return View from ActionFilter

Tags:

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);     } 
like image 912
Mathias F Avatar asked Feb 03 '09 15:02

Mathias F


People also ask

How do I return a view?

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.

How do I redirect an action filter?

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.

What is ActionFilter?

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.

What is ActionFilterAttribute in MVC?

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.


1 Answers

HandleErrorAttribute had what I was looking for.

filterContext.Result = new ViewResult             {                 ViewName = "MessagePage",                 ViewData = filterContext.Controller.ViewData,                 TempData = filterContext.Controller.TempData             }; 
like image 103
Mathias F Avatar answered Oct 17 '22 00:10

Mathias F