ASP.NET路由使用错误的路由生成URL;路由值成为查询字符串参数

时间:2022-05-26 11:20:59

Relevant route registration code:

相关路线注册码:

routes.MapRoute(
  "QuestionsMostRecent",  
  "questions", 
  new { controller = "questions", action = "most_recent" }
);
routes.MapRoute(
  "ControllerActionFormat", 
  "{controller}/{action}.{format}"
);

Route generation code:

路线生成代码:

Url.RouteUrl(new {
  controller = "questions", 
  action = "most_recent", 
  format = "rss" 
});

I expect to receive "/questions/most_recent.rss", but instead I receive "/questions?format=rss". I realize I can force my expected result by referencing the route name "ControllerActionFormat", but I am curious about why exactly the routing system is matching the first route. Can anyone shed some light on this?

我希望收到“/questions/most_recent.rss”,但我会收到“/ questions?format = rss”。我意识到我可以通过引用路由名称“ControllerActionFormat”强制我的预期结果,但我很好奇为什么路由系统正好匹配第一条路由。任何人都可以对此有所了解吗?

1 个解决方案

#1


1  

Because both of them match, but you have the more broadly defined route registered first. Register the more specific route first and it will solve the issue.

因为它们都匹配,但您首先注册的路径更广泛。首先注册更具体的路线,它将解决问题。

#1


1  

Because both of them match, but you have the more broadly defined route registered first. Register the more specific route first and it will solve the issue.

因为它们都匹配,但您首先注册的路径更广泛。首先注册更具体的路线,它将解决问题。