RedirectResult + Object as a parameter in the url

I am developing a fully functional web application and I am using the ASP.NET MVC 3 framework. I am implementing a subclass ActionFilterAttribute.

I redefine the method OnActionExecuting. If an exception hits the method OnActionExecuting, I want to redirect the client browser. The redirect URL targets an action method in one of my controllers. I want to pass data from an object Exceptionto a redirect URL.

Is there a way to create a url including an object Exceptionand then pass the url to the constructor RedirectResult?

+5
source share
3 answers

Is there a way to create a URL, including an Exception object, and then pass the URL to the RedirectResult constructor?

. , , :

var values = new RouteValueDictionary(new
{
    action = "foo",
    controller = "bar",
    exceptiontext = "foo bar baz"
});
filterContext.Result = new RedirectToRouteResult(values);

:

public Action Foo(string exceptionText)
{
    ...
}
+7

TempData .

TempData["MyException"] = myException, , TempData , .

+3

[HandleError]. , , "". , , - , [HandleError], ASP.NET MVC , , .

:

[HandleError]
public class FooController : Controller {
    ...
}

FooController ASP.NET MVC ~/Views/Foo Error ( Error.aspx ASP.NET Error.cshtml ). , ~/Views/Shared.

In your view, you can display a general error as well as display exception information.

For more information on the [HandleError] attribute, see http://blogs.msdn.com/b/gduthie/archive/2011/03/17/get-to-know-action-filters-in-asp-net-mvc-3- using-handleerror.aspx .

+2
source

All Articles