如何更改在提交时格式化Url的方式 [英] How to change the way the Url is formatted on a submit
问题描述
我在Razor模板中有一个搜索框:
I have a search box in a Razor template:
@{
using (Html.BeginForm("Detail", "Book", FormMethod.Get))
{
@Html.TextBox("Id")
<input type="submit" value="Search" />
}
}
当我提交搜索时,它会转到如下网址:
When I submit a search it goes to a url like:
〜/Book/Detail?Id = 1234
~/Book/Detail?Id=1234
但是我希望它像这样格式化网址,只是因为我认为它看起来更干净:
However I want it to format the url like so, just because I think it looks cleaner:
〜/Book/Detail/1234
~/Book/Detail/1234
这可以很好地工作,因为控制器方法签名如下所示:
Which works perfectly fine because the controller method signature looks like this:
// GET: /Book/Detail/id
public ActionResult Detail(string id)
带有TextBoxFor的模型
我尝试了 Html.TextBoxFor
:
@model WebApplication.Models.SearchModel
@{
using (Html.BeginForm("Detail", "Book", FormMethod.Get))
{
@Html.TextBoxFor(m => m.Id)
<input type="submit" value="Search" />
}
}
结果相同.
推荐答案
您使用 GET
请求.这意味着所有参数都将出现在url框中.我现在无法检查,但我想您可以使用以下选项:
You use a GET
request. This means that all parameters will appear in the url box.
I can't check now, but I suppose you could use these options:
- IIS网址重写- http://www.iis.net/downloads/microsoft/url-rewrite
- 通过web.config进行网址重写- http://www.hanselman.com/blog/使用IISRewriteModule.aspx将ASPNETLegacyURLs重定向到Extensionless
还有一批愚蠢的方法:
- You can change your request to
POST
and then modificate the Url by the JS - Modify the URL without reloading the page - You can redirect the request
还,您是否尝试为搜索网址添加个人路由?
Also, did you try to add a personal routing for the search url?
这篇关于如何更改在提交时格式化Url的方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!