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.
[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)]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.