views:

18

answers:

1

I have the following route:

routes.MapRoute(
            "PlayerSearch",
            "Players/{playername}",
            new {controller = "Players", action = "Get"});    

This works if I go to http://mydomain/players/playername.

I also have a form that allows users to look up players by name:

<% using (Html.BeginForm("Get", "Players"))
                    {
                    %>
                    <%=Html.Label("player name")%>
                    <%=Html.TextBox("playername")%>
                    <input type="submit" value="submit" /> 
                <%
                    }%>

This works but the URL is now http://mydomain/players/Get. I want it to be the same URL as the direct URL above. I'm sure this is ignorance (and probably a duplicate but I can't find it) on my part but I just can't get it to work. How do I use routing to get the form to display the desired URL?

A: 

well, seems you need to do something like this:

<% using (Html.BeginRouteForm("PlayerSearch", FormMethod.Post))
                {
                %>
                <%=Html.Label("player name")%>
                <%=Html.TextBox("playername")%>
                <input type="submit" value="submit" /> 
            <%
                }%>

And you have to tell the route that playername is optional:

routes.MapRoute(
        "PlayerSearch",
        "Players/{playername}",
        new { controller = "Players", action = "Get", playername = UrlParameter.Optional });   

That should do the trick. Hope this helps! :)

Yngve B. Nilsen