0

I need to change all the URLs in my MVC project, and thus far this has been quite simple - I've only had to change the URL string in all appropriate routes.MapRoute() in the global.asax.cs file.

One link in particular is acting stubborn and won't change, and I'm sure it's because it is the only one with parameters.

The mapRoute in question is:

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

And the button calling this mapRoute is:

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

When this button is pressed, the URL it requests is: http://www.apps.mysite.net/Mortgage/GetThisRate/8/48/200000/1 - completely ignoring the URL string in its mapRoute method, it would seem.

I've tried placing this mapRoute() at the top of global.asax.cs, to ensure it's not being ignored by a higher priority route, but the same problem persists.

Anybody knowledgeable in MVC routing able to puzzle out what's wrong here?

tereško
  • 58,060
  • 25
  • 98
  • 150
valen
  • 807
  • 1
  • 16
  • 43

3 Answers3

2

forgot the groupId parameter, should be like this (already tested and works fine)

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

And to build the link with the helper you must especifiy the name of route to want use

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

so, you link looks like...

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

I see you're using a custom controls, so can generate the url of action with @URL.RouteUrl and give to your custom control like html parameter

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

extends answer example case, you need a action link with title "click me!", href to the controller "home", action "sayHello", class attribute "some_css" and html id "link_say_hello" solution

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

html output

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

Why attibute class has @??? because class is a reserved word the compiler will throw the following error Compiler Error Message: CS1513: } expected keep in mind.

that's great! but in this case you need a custom route, not the default {controller}/{action} and use with your custom helper so this is when need @URL.RouteLink and give href like html attribute...

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

output

<a class="some_css" href="/mortgage-rates/mortgage-rate-details/8/48/200000/1" id="link_say_hello">click me!</a>
Zach dev
  • 1,610
  • 8
  • 15
  • sorry, your problem was at generate the url and define the route, you must use `@URL.routeURL` edited answer – Zach dev Jun 04 '12 at 19:03
  • Great! The Html.RouteLink worked! I'm a bit confused about how to pass Url.RouteUrl to the OmnitureButton custom control. Would you be able to clarify how? I'll try getting it to work in the meantime – valen Jun 04 '12 at 19:17
0

Looks like name of route "Mortgage.GetThisRate" does not match name you use in Action "GetThisRate" (also it custom helper so may be adding "Mortgage." automatically).

Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
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")
 }
valen
  • 807
  • 1
  • 16
  • 43