0

A standard ActionLink would 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 ActionLinks 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?

Kieran Senior
  • 17,960
  • 26
  • 94
  • 138

2 Answers2

0

Sorry, it doesn't work. Answered here:

How to make ASP.NET Routing escape route values?

Community
  • 1
  • 1
Andreas Eriksson
  • 8,979
  • 8
  • 47
  • 65
  • I'm not sure how this bears any relevance? We're not talking about forward slashes here at all. The routes above are perfectly valid by default. I'm talking about GET requests in forms matching the route constraints defined in Global.asax – Kieran Senior Jun 10 '11 at 12:18
0

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

David Fox
  • 10,603
  • 9
  • 50
  • 80