为什么我的路线表现不如预期?

1 asp.net-mvc routes

我在控制器中有3个动作.我希望前两个是/posts/new,而最后一个是/posts/{filter}?page=N

//all are in the PostsController
[HttpGet]
public ActionResult New()

[HttpPost]
public ActionResult New(PostView post)

[HttpGet]
public ActionResult Browse(string filter, int page)
Run Code Online (Sandbox Code Playgroud)

我现在定义的路线是:

routes.MapRoute("BrowsePosts",
    "posts/{filter}",
    new { controller = "posts", action = "browse", filter = "", page = 1 },
    new { controller = "posts", action = "browse", page = @"\d+" });

routes.MapRoute("NewPost",
    "posts/new",
    new { controller = "", action = "" },
    new { controller = "posts", action = "new" });
Run Code Online (Sandbox Code Playgroud)

我认为我对它们施加的限制可以解决问题,但是请求都是通过第一条路径发送的.我究竟做错了什么?

小智 5

您的"NewPost"路线需要先行,因为该路线将与您的"BrowsePosts"路线相匹配.

按顺序分析路线,并在第一场比赛中分析路线,即使用的路线.

首先映射你的"NewPost"路线,它应该是固定的.