Can I redirect to an action and add some querystring parameters to the url? I noticed the RedirectToAction(object values) overload, but I don't understand how that works.
PS. How can there be a RedirectToAction(object) and a RedirectToAction(object) overload. A string is an object right? Notice that ctrl-shift-space inside the method call when using a string does not trigger the overloads in Visual Studio either!
I can't find the thread, the search is kind of broken for me. But there was a request to include non-matched parameters as query string variables. So if you had a route with {Controller"foo", Action="bar" id=1, page=2} and page was not a route parameter
then your url would be something like this http://localhost/foo/bar/1?page=2. Rob created a ticket for it, not sure what the status is, and I would assume the ticket would cover redirects as well as Action Links. As both would call into RouteCollection.GetUrl()
RedirectToAction(object) and a RedirectToAction(object) overload.
Not sure what you see, but I see three overloads on RedirectToAction. object and string and string, string
All three have unique signatures so they get compiled fine. The two string methods just create anonymous types that delegate to the object overload.
random0xff
A string is an object right?
Yes, but a string is also a string. The compiler picks the best match so using a string calls the string overload. And using an anonymous type calls the object overload. You could call RedirectToAction((object)"goose") which would break at runtime but
that would call the object overloaded method directly because of the cast from string to object.
Google method overload and I am sure you will fined millions of hits explaining how it works.
Yeah there are a few issues with the way RouteCollection.GetUrl and the MCVToolkit generate Urls
RouteCollection.GetUrl doesn't always output the right thing and the MVC Toolkit does some of its own Url generation which is broken.
Effectively what should be happening is RouteCollection.GetUrl should always output the right Url and the MVCToolkit should always delegate Url generation to that.
PS I've posted in another forum about the forum Search function being broken but it doesn't seem like anyone's going to do anything about it any time soon.
random0xff
Member
102 Points
211 Posts
RedirectToAction with querystring?
Jan 08, 2008 01:56 PM|LINK
Can I redirect to an action and add some querystring parameters to the url? I noticed the RedirectToAction(object values) overload, but I don't understand how that works.
PS. How can there be a RedirectToAction(object) and a RedirectToAction(object) overload. A string is an object right? Notice that ctrl-shift-space inside the method call when using a string does not trigger the overloads in Visual Studio either!
rjcox
Contributor
7064 Points
1444 Posts
Re: RedirectToAction with querystring?
Jan 08, 2008 03:05 PM|LINK
I don't think so (HttpRequest.QueryString is a read only collection).
But why are you trying to do this? The HttpRequest that triggered this server action in controller and view will not get to the client.
How about using Controller.ControllerContext.HttpContent.Items?
abombss
Member
575 Points
164 Posts
Re: RedirectToAction with querystring?
Jan 08, 2008 04:43 PM|LINK
I can't find the thread, the search is kind of broken for me. But there was a request to include non-matched parameters as query string variables. So if you had a route with {Controller"foo", Action="bar" id=1, page=2} and page was not a route parameter then your url would be something like this http://localhost/foo/bar/1?page=2. Rob created a ticket for it, not sure what the status is, and I would assume the ticket would cover redirects as well as Action Links. As both would call into RouteCollection.GetUrl()
A workaround for now is to call.
string url = RouteCollection.GetUrl(this.ControllerContext, {Action="bar", id=1});
HttpContext.Response.Redirect(url + "?page=2")
abombss
Member
575 Points
164 Posts
Re: RedirectToAction with querystring?
Jan 08, 2008 04:49 PM|LINK
Not sure what you see, but I see three overloads on RedirectToAction. object and string and string, string
All three have unique signatures so they get compiled fine. The two string methods just create anonymous types that delegate to the object overload.
Yes, but a string is also a string. The compiler picks the best match so using a string calls the string overload. And using an anonymous type calls the object overload. You could call RedirectToAction((object)"goose") which would break at runtime but that would call the object overloaded method directly because of the cast from string to object.
Google method overload and I am sure you will fined millions of hits explaining how it works.
tgmdbm
Contributor
4392 Points
883 Posts
ASPInsiders
MVP
Re: RedirectToAction with querystring?
Jan 08, 2008 08:34 PM|LINK
Yeah there are a few issues with the way RouteCollection.GetUrl and the MCVToolkit generate Urls
RouteCollection.GetUrl doesn't always output the right thing and the MVC Toolkit does some of its own Url generation which is broken.
Effectively what should be happening is RouteCollection.GetUrl should always output the right Url and the MVCToolkit should always delegate Url generation to that.
The post referred to by Adam is http://forums.asp.net/t/1197244.aspx
PS I've posted in another forum about the forum Search function being broken but it doesn't seem like anyone's going to do anything about it any time soon.
Haacked
Contributor
6901 Points
412 Posts
Re: RedirectToAction with querystring?
Jan 09, 2008 06:42 AM|LINK
We have plans to append "overflow" parameters as query string parameters. By "overflow" I mean this:
Suppose you have the route: /[controller]/[action]/[id]
But you generate an URL like so: RedirectToAction(new {controller="home", action="index", id=123,somethingelse=abc}
The URL generated would be: /home/index/123?somethingelse=abc
Since "somethingelse" is not defined as a "parameter" in the URL, it becomes a query string parameter when generating the route.
Senior Program Manager, Microsoft
What wouldn’t you do for a Klondike bar?
rjcox
Contributor
7064 Points
1444 Posts
Re: RedirectToAction with querystring?
Jan 09, 2008 09:16 AM|LINK
Excellent, will allow easy extensibility without the need to define lots of "one use" routes.
random0xff
Member
102 Points
211 Posts
Re: RedirectToAction with querystring?
Jan 09, 2008 11:00 AM|LINK
Ok, thanks for clearing that up. Hard to discover the API when the method expects object though.
RyanWyan
Member
2 Points
1 Post
Re: RedirectToAction with querystring?
Jun 01, 2009 07:30 AM|LINK
RedirectToAction("ActionName", "ControllerName", new { parameterId1 = parameterValue1, parameterId2 = parameterValue2 })
e.g., RedirectToAction("Index", "Home", new { id = 22 }) = ~/Home/index/id/22
"ASP.NET MVC"
Software Engineer,
Islamabad Pakistan.