Reputation: 1250
Hi I have my mvc app and this code snippet:
protected override void OnException(ExceptionContext filterContext)
{
base.OnException(filterContext);
ActionInvoker.InvokeAction(filterContext, "ErrorMessage");
}
public ActionResult ErrorMessage(ExceptionContext filterContext)
{
ViewModel<Exception> viewModel = ViewModelFactory.CreateFor(filterContext.Exception);
return View(viewModel);
}
The problem is that I can't pass arguments to method. I thought it would be this filterContext but in ErrorMessage method it has all default values.
So my question is - How to pass some values to method that I invoke?
Upvotes: 2
Views: 2066
Reputation: 1250
I don't know if it was clear from my question but what I wanted achive was that I didn't wanted to catch any exception in my controllers actions and still get good-looking message about error. So my solution which satisfies me is:
protected override void OnException(ExceptionContext filterContext)
{
base.OnException(filterContext);
TempData.Add(MyCommons.Exception, filterContext.Exception);
var controllerName = filterContext.RequestContext.RouteData.Values["Controller"];
filterContext.Result =
new RedirectToRouteResult(new RouteValueDictionary(new {controller = controllerName, action = "Error"}));
filterContext.ExceptionHandled = true;
filterContext.HttpContext.Response.Clear();
}
Upvotes: 2
Reputation: 5124
You dont pass parameters to the invoked action, use model binding instead.
If you want to pass some custom parameters out of default model binding sources (query, form, route..), wrap them in some class and implement your custom model binder for it (or valueprovider, but i think modelbinder is more appropriate here).
Upvotes: 1