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.

6
  • can you also show how the route is defined in your routeconfig? Commented Mar 17, 2017 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. Commented Mar 17, 2017 at 14:11
  • Could you try [Route("connections/{city:name}/map/{page?}]? Commented Mar 17, 2017 at 14:28
  • Still doesn't work. Commented Mar 17, 2017 at 14:32
  • 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 nulls. 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. Commented Nov 29, 2017 at 10:07

1 Answer 1

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)]
Sign up to request clarification or add additional context in comments.

5 Comments

Thank you for your answer, but even when I remove the name constraint, I still get null, I tried that already...
Should of worked, however I am curious do you have a similar routing in your controller that may be causing a conflict?
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.
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
@Url.Action("Connections", new { city = "madrid", page = 1 }) always returns null, that's what the question is about..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.