创建与表单中的路由匹配的 URL
我有以下路线:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,似乎你需要做这样的事情:
并且你必须告诉路线,玩家名是可选的:
这应该可以解决问题。希望这有帮助! :)
well, seems you need to do something like this:
And you have to tell the route that playername is optional:
That should do the trick. Hope this helps! :)