Asp.net MVC URL路由:有参数时更改URL的问题

时间:2012-06-04 17:55:33

标签: c# asp.net-mvc asp.net-mvc-routing global-asax

我需要更改MVC项目中的所有URL,到目前为止这一点非常简单 - 我只需要更改global.asax.cs文件中所有相应路由中的URL字符串.MapRoute() 。

特别是一个链接表现得很顽固,不会改变,我确定这是因为它是唯一一个带参数的链接。

有问题的mapRoute是:

routes.MapRoute(
            "Mortgage.GetThisRate",
            "mortgage-rates/mortgage-rate-details/{groupId}/{paymentType}/{mortgageValue}/{province}",
            new { controller = "Mortgage", action = "GetThisRate", paymentType = UrlParameter.Optional, mortgageValue = UrlParameter.Optional, province = UrlParameter.Optional },
            new { groupId = "\\d+", paymentType = "\\d+", mortgageValue = "\\d+", province = "\\d+" }
        );

调用此mapRoute的按钮是:

 @Html.OmnitureButton( Url.Action("GetThisRate", new { groupId = info.GroupId }), "<span class=\"payment\"></span><br />Get Details", new {@class = "orange"}, "events", "event3", "", "event3", "", "Mortgage LearnMore")

按下此按钮时,它请求的URL为:http://www.apps.mysite.net/Mortgage/GetThisRate/8/48/200000/1 - 完全忽略其mapRoute方法中的URL字符串,它似乎就是。

我已经尝试将此mapRoute()放在global.asax.cs的顶部,以确保它不会被更高优先级的路由忽略,但同样的问题仍然存在。

任何了解MVC路由的人都能解开这里的错误吗?

3 个答案:

答案 0 :(得分:2)

忘记了groupId参数,应该是这样的(已经过测试并且工作正常)

routes.MapRoute(
        "Mortgage.GetThisRate",
        "mortgage-rates/mortgage-rate-details/{groupId}/{paymentType}/{mortgageValue}/{province}",
        new { controller = "Mortgage", action = "GetThisRate", groupId = UrlParameter.Optional, paymentType = UrlParameter.Optional, mortgageValue = UrlParameter.Optional, province = UrlParameter.Optional },
        new { groupId = "\\d+", paymentType = "\\d+", mortgageValue = "\\d+", province = "\\d+" }

要与帮助者建立链接,您必须指定要使用的路径名称

@Html.RouteLink("title of link", "Mortgage.GetThisRate", new { groupId = "8", paymentType = "48", mortgageValue = "200000", province = "1" })

所以,你的链接看起来像......

yoursite/mortgage-rates/mortgage-rate-details/8/48/200000/1

我看到你正在使用自定义控件,因此可以使用@ URL.RouteUrl生成操作网址并将其提供给自定义控件,例如html参数

@Url.RouteUrl("Mortgage.GetThisRate", new { groupId = "8", paymentType = "48", mortgageValue = "200000", province = "1" })

扩展回答 例子, 你需要一个标题为“click me!”的动作链接,href到控制器“home”,动作“sayHello”,类属性“some_css”和html id“link_say_hello” 溶液

@Html.ActionLink("click me!", "sayHello", "home", null, new { id="link_say_hello", @class = "some_css"})

html输出

<a class="some_css" href="/home/sayHello" id="link_say_hello">click me!</a>

为什么attibute类有@ ???因为类是保留字,编译器将抛出以下错误编译器错误消息:CS1513:}预期 请记住。

太棒了!但在这种情况下,您需要一个自定义路由,而不是默认的{controller} / {action},并与您的自定义帮助程序一起使用 所以这是需要@ URL.RouteLink并给href像html属性......

new {href = @Url.RouteUrl("Mortgage.GetThisRate", new { groupId = "8", paymentType = "48", mortgageValue = "200000", province = "1", id="link_say_hello", @class = "some_css" })}

输出

<a class="some_css" href="/mortgage-rates/mortgage-rate-details/8/48/200000/1" id="link_say_hello">click me!</a>

答案 1 :(得分:0)

看起来像路线的名字&#34; Mortgage.GetThisRate&#34;与您在Action&#34; GetThisRate&#34;中使用的名称不匹配(也是自定义助手,因此可能会自动添加&#34;抵押。&#34;。

答案 2 :(得分:0)

解决方案是使用Url.RouteUrl()并将其返回值转储到Html.OmnitureButton()中。 mapRoute()可选参数需要更改为默认值,因为RouteUrl()不适用于选项。

工作代码如下:

routes.MapRoute(
           "Mortgage.GetThisRate",
           "mortgage/mortgage-rate-details/{groupId}/{paymentType}/{mortgageValue}/{province}",
           new { controller = "Mortgage", action = "GetThisRate", paymentType = 48, mortgageValue = 200000, province = 1 },
           new { groupId = "\\d+", paymentType = "\\d+", mortgageValue = "\\d+", province = "\\d+" }
       );

按钮:

@{
  string x = Url.RouteUrl("Mortgage.GetThisRate", new { groupId = info.GroupId});
  @Html.OmnitureButton(x, "<span class=\"payment\"></span><br />Get Details", new {@class = "orange"}, "events", "event3", "", "event3", "", "Mortgage LearnMore")
 }