0

我需要更改我的 MVC 项目中的所有 URL,到目前为止,这非常简单——我只需要更改 global.asax.cs 文件中所有适当的 routes.MapRoute() 中的 URL 字符串。

特别是其中一个环节表现得很顽固,不会改变,我敢肯定这是因为它是唯一一个有参数的环节。

有问题的 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 路由的人都能够弄清楚这里出了什么问题?

4

3 回答 3

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 生成操作 url 并提供给您的自定义控件,如 html 参数

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

扩展答案 示例案例,您需要一个标题为“点击我!”的操作链接,指向控制器“home”的href,操作“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>

为什么属性类有@??? 因为 class 是保留字,所以编译器会抛出以下错误 Compiler Error Message: CS1513: } expected 请记住。

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

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>
于 2012-06-04T18:08:28.240 回答
0

The solution was to use Url.RouteUrl() and dump its return value into Html.OmnitureButton(). The mapRoute() optional parameters needed to be changed to defaults, as RouteUrl() did not work with optionals.

The working code looks like:

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+" }
       );

And the button:

@{
  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")
 }
于 2012-06-04T21:43:52.770 回答
0

看起来路线“Mortgage.GetThisRate”的名称与您在操作“GetThisRate”中使用的名称不匹配(它也是自定义助手,因此可能会自动添加“Mortgage.”)。

于 2012-06-04T18:04:12.057 回答