0

我指定了以下路由 - MyHttpMethodConstraint 是一种检查由变量覆盖的 HTTP 方法的路由。

routes.MapRoute("Retrieve", "{controller}/{id}/{action}", new { action = "retrieve" }, new { action = "create|retrieve|update|delete", httpMethod = new MyHttpMethodConstraint("GET", "HEAD"), id = new GuidRouteConstraint() });
routes.MapRoute("Update", "{controller}/{id}", new { action = "update" }, new { action = "update", httpMethod = new MyHttpMethodConstraint("PUT"), id = new GuidRouteConstraint() });
routes.MapRoute("Delete", "{controller}/{id}", new { action = "destroy" }, new { action = "delete", httpMethod = new MyHttpMethodConstraint("DELETE"), id = new GuidRouteConstraint() });
routes.MapRoute("Create", "{controller}", new { action = "create" }, new { action = "create", httpMethod = new MyHttpMethodConstraint("POST") });

Everthing 被正确路由到 Actions,但是 Url.ActionLink 的 URL 生成并没有像我希望的那样工作(使用受限于 HTTP GET 方法的路由),而是找到那些受限于 POST/PUT/DELETE 的路由,因此错误的网址。我尝试重新排序路线,但这无济于事。我希望 URL 生成忽略约束。

除了构建我自己的 ActionLink 方法之外,是否有解决方法?

编辑

列表底部还有一条默认路由:

routes.MapRoute("Default", "{controller}/{action}", new { controller = "home", action = "index" });
4

1 回答 1

1

问题已解决 - 问题在于它不适用于指向创建操作的链接(即,当 GET 时),因为它没有使用上述任何路由,而是底部的默认路由(没有任何约束)。我的有效路线列表(包括默认路线)是:

routes.MapRoute("Retrieve/UpdateSetup/DeleteSetup", "{controller}/{id}/{action}", new { action = "retrieve" }, new { action = "retrieve|update|delete", httpMethod = new MyHttpMethodConstraint("GET", "HEAD"), id = new GuidRouteConstraint() });
routes.MapRoute("CreateSetup", "{controller}/create", new { action = "create" }, new { action = "create", httpMethod = new MyHttpMethodConstraint("GET", "HEAD") });
routes.MapRoute("Update", "{controller}/{id}", new { action = "update" }, new { action = "update", httpMethod = new MyHttpMethodConstraint("PUT"), id = new GuidRouteConstraint() });
routes.MapRoute("Delete", "{controller}/{id}", new { action = "delete" }, new { action = "delete", httpMethod = new MyHttpMethodConstraint("DELETE"), id = new GuidRouteConstraint() });
routes.MapRoute("Create", "{controller}", new { action = "create" }, new { action = "create", httpMethod = new MyHttpMethodConstraint("POST") });

routes.MapRoute("Default", "{controller}/{action}", new { controller = "home", action = "index" });
于 2012-04-13T19:23:38.160 回答