I am developing a full-web application and I am using the ASP.NET MVC 3 framework. I am implementing a subclass of ActionFilterAttribute
.
I am overriding the OnActionExecuting
method. If an exception is caught in OnActionExecuting
method I want to redirect the client browser. The redirection URL targets an action method in one of my controllers. I want to pass data from the Exception
object into the redirection URL.
Is there a way to build a URL including the Exception
object and then passing the URL into the RedirectResult
constructor?
RedirectToAction will return a http 302 response to the browser and then browser will make GET request to specified action. Save this answer. Show activity on this post. Ideally I would use RedirectToRoute for Action Links/Images and RedirectToAction in Controller's Action to redirect to another Controller's Action .
How do I pass multiple parameters in RedirectToAction? Second, to pass multiple parameters that the controller method expects, create a new instance of RouteValueDictionary and set the name/value pairs to pass to the method.
Action method only creates the url not the complete hyperlink, to create hyperlink we need to use Html. ActionLink covered next. To access these querystring values in the action method, we can use Request.QueryString like below. CONTROLLER ACTION METHOD public ActionResult Index() { string com = Request.
When the Send Button is clicked, data from the View is received in the PersonModel class object as parameter. Finally, the PersonModel class object is passed to the RedirectToAction method along with the name of the destination Controller and its Action method.
You can use TempData for situations like this.
Just set TempData["MyException"] = myException
before you redirect and then check for that TempData value in the action you redirect to.
Is there a way to build a URL including the Exception object and then passing the URL into the RedirectResult constructor ?
No. You can pass only query string parameters like for example:
var values = new RouteValueDictionary(new
{
action = "foo",
controller = "bar",
exceptiontext = "foo bar baz"
});
filterContext.Result = new RedirectToRouteResult(values);
and in the target action you will be able to fetch the exception text parameter:
public Action Foo(string exceptionText)
{
...
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With