I was trying to clean up my exception handling method by following some advice from another thread and assigning the Reult of the filterContext. Here's the code to make it clear:
So the idea was to use an ActionResult method instead of having to render a View by string path assignment. I just noticed that I never replaced the "PartialView" with "View" when I made this a complete page, but that doesn't matter for the moment. The problem I'm having is that when this line runs:
filterContext.Result = RedirectToAction("MyError", new { exception = myException });
The 'exception' variable has the correct values assigned. When my ActionResult method executes, the variable is null. Here's the very simple ActionResult method:
public ActionResult MyError(MyException exception)
{
return View(exception);
}
I've had this problem before and never was able to resolve it. I can pass primitive types all day long without the slightest problem, but if I pass one of my classes as an argument to an ActionResult, it's always null when the method executes. Thinking it might have been a routing issue, I added this route:
But it didn't help in the slightest. I changed the name, just in case naming the route the same as the action would cause a problem, but this also had no effect. When calling a non-ActionResult method, I can pass any type of arguments I like. Can someone
please shed a bit of light on why I can't do this with an ActionResult method?
One thing you can try is.. store the exception context in TempData, then redirct o your action and access the exception from TempData...
Its wierd, as redirect doesn't seem to work from an exception handler with proper exception value... Anyone else got this working?
An extract is shown below.....
protectedoverridevoid OnException(ExceptionContext filterContext){// Make use of the exception laterthis.TempData["ErrorException"]= filterContext.Exception; // Mark exception as handled
filterContext.ExceptionHandled=true;// ... logging, etc// Redirect
filterContext.Result=this.RedirectToAction("Error", "Home");base.OnException(filterContext);}
I blog at http://rajeshpillai.net and have a community startup http://ownabook.org/
(Don't forget to click "Mark as Answer" on the post(s) that helped you.)
Thanks, but the problem was passing user-defined types into ActionResult methods. I can handle the exceptions as it is currently written. I just need to know what to do to allow a class instance to be passed into an ActionResult method. No matter what I've
ever tried, they always end up as null when the method is called.
Ironwil
Member
76 Points
96 Posts
Passing user-defined types as ActionResult arguments
Mar 19, 2012 07:26 PM|LINK
I was trying to clean up my exception handling method by following some advice from another thread and assigning the Reult of the filterContext. Here's the code to make it clear:
protected override void OnException(ExceptionContext filterContext) { filterContext.ExceptionHandled = true; string controller = filterContext.RouteData.Values["controller"].ToString(); string action = filterContext.RouteData.Values["action"].ToString(); MyException myException = new MyException { Controller = controller, Action = action, Exception = filterContext.Exception.Message, InnerException = filterContext.Exception.InnerException != null ? filterContext.Exception.InnerException.Message : "none" }; filterContext.Result = RedirectToAction("MyError", new { exception = myException }); // this.PartialView("~/Views/Shared/MyError.aspx", myException).ExecuteResult(this.ControllerContext); }So the idea was to use an ActionResult method instead of having to render a View by string path assignment. I just noticed that I never replaced the "PartialView" with "View" when I made this a complete page, but that doesn't matter for the moment. The problem I'm having is that when this line runs:
filterContext.Result = RedirectToAction("MyError", new { exception = myException });The 'exception' variable has the correct values assigned. When my ActionResult method executes, the variable is null. Here's the very simple ActionResult method:
public ActionResult MyError(MyException exception) { return View(exception); }I've had this problem before and never was able to resolve it. I can pass primitive types all day long without the slightest problem, but if I pass one of my classes as an argument to an ActionResult, it's always null when the method executes. Thinking it might have been a routing issue, I added this route:
routes.MapRoute("MyError", "MyError/{exception}", new { controller = "ControllerBase", action = "MyError", exception = UrlParameter.Optional });But it didn't help in the slightest. I changed the name, just in case naming the route the same as the action would cause a problem, but this also had no effect. When calling a non-ActionResult method, I can pass any type of arguments I like. Can someone please shed a bit of light on why I can't do this with an ActionResult method?
thinkrajesh
Participant
1356 Points
232 Posts
Re: Passing user-defined types as ActionResult arguments
Mar 20, 2012 04:16 AM|LINK
One thing you can try is.. store the exception context in TempData, then redirct o your action and access the exception from TempData...
Its wierd, as redirect doesn't seem to work from an exception handler with proper exception value... Anyone else got this working?
An extract is shown below.....
(Don't forget to click "Mark as Answer" on the post(s) that helped you.)
Ironwil
Member
76 Points
96 Posts
Re: Passing user-defined types as ActionResult arguments
Mar 22, 2012 05:06 PM|LINK
Thanks, but the problem was passing user-defined types into ActionResult methods. I can handle the exceptions as it is currently written. I just need to know what to do to allow a class instance to be passed into an ActionResult method. No matter what I've ever tried, they always end up as null when the method is called.