我有一个搜索表单,我希望在提交表单时生成一个干净的URL。
我为此创建了一个自定义路线:
routes.MapRoute(
name: "Custom",
url: "{controller}/{action}/{KEY}/{BN}/{SBN}/{CN}",
defaults: new { controller = "Home", action = "Index" }
);
这是我的表格:
@using (Html.BeginForm("TestUrl", "Home", FormMethod.Get))
{
<input type="text" name="KEY" value="q"/>
<input type="text" name="BN" value="Miscellaneous" />
<input type="text" name="SBN" value="Specialized Photographic Equipment" />
<input type="text" name="CN" value="South Georgia and the South Sandwich Islands" />
<input type="submit" value="Search"/>
}
我的行动如下:
public ActionResult TestUrl(string KEY,string BN,string SBN, string CN)
{
return View();
}
当我提交表单时,会生成一个这样的网址:
我的ActionLink
正在生成一个干净的网址:
@Html.ActionLink("Test Link", "TestUrl", "Home", new { KEY = "0", BN = "Miscellaneous", SBN = "Specialized Photographic Equipment", CN = "South Georgia and the South Sandwich Islands" }, new { })
ActionLink
会生成这个干净的网址:
我提交表单时如何删除查询字符串?
答案 0 :(得分:1)
您缺少的是在设置中包含您的路线变量。将它们作为路线的一部分,而且也在匿名物体内,这不仅仅是足够的。
routes.MapRoute(
name: "Custom",
url: "{controller}/{action}/{KEY}/{BN}/{SBN}/{CN}",
defaults: new { controller = "Home", action = "Index",
KEY = "", BN = "", SBN = "", CN = "" }
);