1

This kind of question has been asked before on SO (post 1, post 2, post 3), but none of them was able to help me.

I have the following route (defined inside a LandingController class):

[HttpGet]
[Route("connections/{city:name}/map{page?}", Order = 2)]
public Task<ActionResult> Connections(string city, int page = 1)

for which I am trying to generate an URL in my Razor template like this:

@Url.Action("Connections", "Landing", new { city = "madrid", page = 1 })

or

@Url.Action("Connections", new { city = "madrid", page = 1 })

Both these calls return null. Anyone has an idea why?

However if I omit the page parameter in the route template (doing [Route("connections/{city:name}/map", Order = 2)]), the following URL is generated: /connections/madrid/map/?page=1, which is not what I want, but it shows that I am doing something wrong with my calls above.

Community
  • 1
  • 1
Genti Saliu
  • 2,643
  • 4
  • 23
  • 43
  • can you also show how the route is defined in your routeconfig? – Axel Bouttelgier Mar 17 '17 at 14:10
  • My route is defined using attribute routing, i.e. above the action method (which I have posted in the question), so it's not in a routeconfig. – Genti Saliu Mar 17 '17 at 14:11
  • Could you try `[Route("connections/{city:name}/map/{page?}]`? – juunas Mar 17 '17 at 14:28
  • Still doesn't work. – Genti Saliu Mar 17 '17 at 14:32
  • Have you been able to solve this problem? Would love to see your solution! – Glen Little Nov 28 '17 at 22:22
  • 1
    @GlenLittle I was able to solve this. We use culture-specific URLs depending on `CurrentCulture` and it turned out that the routing info for that specific culture was not set up in the database where we keep our routing configuration, and I kept getting `null`s. Basically, getting null from `@Url.Action` means that the route you are looking for has not been configured and does not exist. I was not aware that we saved route configs in the database at the time. – Genti Saliu Nov 29 '17 at 10:07

1 Answers1

0
[HttpGet]
[Route("connections/{city:name}/map{page?}", Order = 2)]
public Task<ActionResult> Connections(string city, int page = 1)

Pretty sure your routing is incorrect, {city:name} what is name? I don't recall any having a constraint type name.

Your attribute routing should be as simple as this:

[Route("connections/{city}/map/{page?}", Order = 2)]

If you want to add constraint to page as integer it would be:

[Route("connections/{city}/map/{page:int?}", Order = 2)]
penleychan
  • 5,370
  • 1
  • 17
  • 28
  • Thank you for your answer, but even when I remove the `name` constraint, I still get `null`, I tried that already... – Genti Saliu Mar 17 '17 at 15:19
  • Should of worked, however I am curious do you have a similar routing in your controller that may be causing a conflict? – penleychan Mar 17 '17 at 16:01
  • No, I checked. If I call the URL which I am expecting to be generated (`connections/{city:name}/map1`) and I run the debugger, the breakpoint set inside the action implementation is hit - so definitely no conflicts. – Genti Saliu Mar 17 '17 at 16:04
  • Can you confirm that your `@Url.Action("Connections", new { city = "madrid", page = 1 })` is actually generating the correct url as `route data` and not `querystrings`? I got a feeling that it's generating `http://localhost/landing/connections?city=madrid?page=1` – penleychan Mar 17 '17 at 16:38
  • `@Url.Action("Connections", new { city = "madrid", page = 1 })` always returns `null`, that's what the question is about.. – Genti Saliu Mar 17 '17 at 16:41