[英]ASP.NET MVC 5 Attribute Routing: Url.Action returns null when using multiple parameters
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. 之前已经问过这样的问题(第1篇 ,第2篇 ,第3篇 ),但没有一个能够帮助我。
I have the following route (defined inside a LandingController
class): 我有以下路由(在
LandingController
类中定义):
[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: 我试图在我的Razor模板中生成一个URL,如下所示:
@Url.Action("Connections", "Landing", new { city = "madrid", page = 1 })
or 要么
@Url.Action("Connections", new { city = "madrid", page = 1 })
Both these calls return null
. 这两个调用都返回
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. 但是,如果我省略路由模板中的page参数(执行
[Route("connections/{city:name}/map", Order = 2)]
),则会生成以下URL: /connections/madrid/map/?page=1
,这不是我想要的,但它表明我上面的调用我做错了。
[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? 很确定你的路由不正确,
{city: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)]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.