Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RedirectResult + Object as a parameter in a URL

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?

like image 912
user1139666 Avatar asked Jan 25 '12 22:01

user1139666


People also ask

What is difference between RedirectToAction and RedirectToRoute?

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?

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.

What is action method in URL?

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.

How do I pass model in RedirectToAction?

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.


2 Answers

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.

like image 38
Chris Avatar answered Nov 08 '22 18:11

Chris


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)
{
    ...
}
like image 108
Darin Dimitrov Avatar answered Nov 08 '22 17:11

Darin Dimitrov