我指定了以下路由 - 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 /的路由删除,从而错误的URL。我试过重新订购路线,但这没有用。我希望URL生成忽略约束。
除了构建自己的ActionLink方法之外,还有解决方法吗?
修改
列表底部还有一个默认路由:
routes.MapRoute("Default", "{controller}/{action}", new { controller = "home", action = "index" });
答案 0 :(得分: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" });