tags:

views:

118

answers:

3

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);
    }
+2  A: 

Yes. Look at the source for HandleErrorAttribute.

Craig Stuntz
A: 

Try this

[HandleError]
public ActionResult MyAction (int id)
{
    // ...
}

And put the view you want rendered in to ~/Views/Shared/Error.ascx.

Nick Berardi
Filters like [HandleError] apply only to action methods. The OnActionExecuting() method is itself a filter rather than an action method, so attributes like this have no effect.
Levi
Sorry I didn't even look at the method. I have updated.
Nick Berardi
+2  A: 

HandleErrorAttribute had what I was looking for.

filterContext.Result = new ViewResult
            {
                ViewName = "MessagePage",
                ViewData = filterContext.Controller.ViewData,
                TempData = filterContext.Controller.TempData
            };
Malcolm Frexner
I think you should accept your own answer because it includes code
John Sheehan
For whatever it's worth, I intentionally did not include code, since this area of code is changing with every release of the MVC framework right now. I think the correct answer is "do whatever the framework is currently doing."
Craig Stuntz