Marty
Marty

Reputation: 3555

Add Odata query param to @html.Routelink

I have a routelink :

@Html.RouteLink("Campaigns", "DefaultApi", new { controller = "Campaign", httproute = true })

that results in "http://localhost:54614/api/v0.1/ReportData/Account"

what I want to do is write a Routelink - that would result in

"http://localhost:54614/api/v0.1/ReportData/Account/?$top=20"

How can I do that ?

Upvotes: 2

Views: 251

Answers (1)

nemesv
nemesv

Reputation: 139768

You you can't use $top as a property name in an anonymous type so you should use a different Routelink overload which uses RouteValueDictionary.

But the problem is that RouteLink uses Uri.EscapeUriString to escape the route values so using the following code:

@Html.RouteLink("Campaigns", "DefaultApi", new RouteValueDictionary
                                              {
                                                  { "controller", "Campaign"} ,
                                                  { "httproute", true },
                                                  { "$top", 20 }
                                              })

Will produce this url: /api/Campaign?%24top=20 (see the $ was encoded to %24)

However using a very very dirty hack namely Uri.UnescapeDataString:

@Html.Raw(Uri.UnescapeDataString(
    @Html.RouteLink("Campaigns", "DefaultApi", new RouteValueDictionary
                                        {
                                            { "controller", "Campaign"} ,
                                            { "httproute", true },
                                            { "$top", 20 }
                                        }).ToHtmlString()))

You can get: /api/Campaign?$top=20

Upvotes: 1

Related Questions