如何更改在提交时格式化Url的方式 [英] How to change the way the Url is formatted on a submit

查看:118
本文介绍了如何更改在提交时格式化Url的方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在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:

  1. IIS网址重写- http://www.iis.net/downloads/microsoft/url-rewrite
  2. 通过web.config进行网址重写- http://www.hanselman.com/blog/使用IISRewriteModule.aspx将ASPNETLegacyURLs重定向到Extensionless

还有一批愚蠢的方法:

  1. 您可以将请求更改为 POST ,然后通过JS修改网址-
  1. You can change your request to POST and then modificate the Url by the JS - Modify the URL without reloading the page
  2. You can redirect the request

还,您是否尝试为搜索网址添加个人路由?

Also, did you try to add a personal routing for the search url?

这篇关于如何更改在提交时格式化Url的方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆