Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RedirectToAction and RedirectToRoute

In my controller of webpage 1, I want to redirect to Webpage 2, passing 2 variables.

I tried using RedirectToRoute, but cannot get it to work; wrong URL is displayed. I then switched to using RedirectToAction.

my code:

Routing

routes.MapRoute(
    "CreateAdditionalPreviousNames", // Route name
    "Users/{controller}/{action}/{userId}/{applicantId}", // URL with parameters
    new { controller = "UsersAdditionalPreviousNames", action = "Index", userId = UrlParameter.Optional, applicantId = UrlParameter.Optional } // Parameter defaults
);

RedirectToAction (which works)

return RedirectToAction("Index", "UsersAdditionalPreviousNames", new { userId = user.Id, applicantId = applicant.Id });

RedirectToRoute (doesn't work)

return RedirectToRoute("CreateAdditionalPreviousNames", new { userId = user.Id, applicantId = applicant.Id });

Oh, and one other thing, can you make parameters required, rather than optional....if so, how?

like image 670
user1079925 Avatar asked Jan 20 '12 16:01

user1079925


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. 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 .

What is RedirectToRoute in MVC?

RedirectToRoute(Object) Redirects to the specified route using the specified route values. RedirectToRoute(String) Redirects to the specified route using the route name.

What does RedirectToAction do in MVC?

RedirectToAction(String, String, Object)Redirects to the specified action using the action name, controller name, and route dictionary.

How do I use RedirectToAction?

The RedirectToAction() MethodThis method is used to redirect to specified action instead of rendering the HTML. In this case, the browser receives the redirect notification and make a new request for the specified action. This acts just like as Response. Redirect() in ASP.NET WebForm.

What is the difference between redirecttoroute and redirect to action?

Redirect to route looks up the route table thats defined in global.asax and redirect to action redirects you to a specified controller/action. Show activity on this post. RedirectToRoute means it redirects to a specific URL which is defined in routing API. (Global.asax)

What is return redirecttoaction in ASP NET?

return RedirectToAction () To redirect to a different action which can be in the same or different controller. It tells ASP.NET MVC to respond with a browser to a different action instead of rendering HTML as View () method does. Browser receives this notification to redirect and makes a new request for the new action.

How do I redirect to a specific action in HTML?

The RedirectToAction() Method. This method is used to redirect to specified action instead of rendering the HTML. In this case, the browser receives the redirect notification and make a new request for the specified action.

What is redirecttorouteresult in ASP NET MVC?

It is rendered to the page by URL. If we give the wrong URL, it will show 404-page errors. The RedirectToRouteResult is used whenever we need to go from one action method to another action method within the same or different controller in ASP.NET MVC Application.


1 Answers

Omit parameter defaults to make parameters required:

    routes.MapRoute(     "CreateAdditionalPreviousNames", // Route name     "Users/{controller}/{action}/{userId}/{applicantId}", // URL with parameters     new { controller = "UsersAdditionalPreviousNames", action = "Index" } ); 

For route redirect, try this:

return RedirectToRoute(new  {      controller = "UsersAdditionalPreviousNames",      action = "Index",      userId = user.Id,      applicantId = applicant.Id  }); 

Another habit I picked up from Steve Sanderson is not naming your routes. Each route can have a null name, which makes you specify all parameters explicitly:

    routes.MapRoute(     null, // Route name     "Users/{controller}/{action}/{userId}/{applicantId}", // URL with parameters     new { controller = "UsersAdditionalPreviousNames", action = "Index" } ); 
like image 128
danludwig Avatar answered Sep 20 '22 12:09

danludwig