创建与表单中的路由匹配的 URL

发布于 2024-09-13 23:44:13 字数 884 浏览 5 评论 0原文

我有以下路线:

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

如果我转到 http://mydomain/players/playername,则此路线有效。

我还有一个表单,允许用户按名称查找玩家:

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

这可以工作,但 URL 现在是 http://mydomain/玩家/获取。我希望它与上面的直接 URL 相同。我确信这是我的无知(可能是重复的,但我找不到它),但我就是无法让它工作。如何使用路由让表单显示所需的 URL?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

删除→记忆 2024-09-20 23:44:13

好吧,似乎你需要做这样的事情:

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

并且你必须告诉路线,玩家名是可选的:

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

这应该可以解决问题。希望这有帮助! :)

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! :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文