GET Request Route Parameters
A standard ActionLink
wou开发者_如何学运维ld produce something like mysite.com/rar/hello/1/2010-01-01/ if you wanted it to use route magic. This is fine for anchor tags and using Html.ActionLink
, however, what if I wanted to have a form using a GET request to have the same effect? So a form looking like the following:
@using(Html.BeginForm("MyAction", "MyController", new { id = 20, something = "2010-01-01" }))
Yielding a URL like site.com/20/2010-01-01 instead of mysite.com?id=20&something=2010-01-01
Can this be done without using javascript? My routes are correct because ActionLink
s generate the correct URL, but with the same parameters a GET request through a form generates the ugly URL.
Cheers
--- EDIT ---
I did a search in Google for this same problem, and guess what? This answer is the first one. I hadn't even remembered asking it.
So I've come across the same problem again. It's as simple as this:
I click on a link, it takes me to a URL like site.com/10/100/Param
. I have a form with these same route values that looks something like this:
@using(Html.BeginForm("Action", "Controller", FormMethod.Get)) {
@Html.Hidden("param1", 10)
@Html.Hidden("param2", 110)
@Html.Hidden("param3", "MyOtherParam")
}
Then when I submit this form, I expect to see the new route as site.com/10/110/MyOtherParam
instead.
The only way I can think of doing it is POSTing to an action method and then redirecting from there.
Any ideas?
Sorry, it doesn't work. Answered here:
How to make ASP.NET Routing escape route values?
Have you explicitly described this route in global.asax? You would need an entry like
routes.MapRoute(
"NameOfRoute", // Route name
"{controller}/{action}/{id}/{something}", // URL with parameters
new { controller = "My", action = "MyController" }
);
to completely match the ActionLink above
精彩评论