我希望看到一种处理路由的新方法,以便更轻松地开发 REST 服务。目前我有这样的路线:
context.MapRoute(null,
"api/posts",
new { controller = "Post", action = "Get" },
new { httpConstraint = new HttpMethodConstraint("GET") });
context.MapRoute(null,
"api/posts",
new { controller = "Post", action = "Insert" },
new { httpConstraint = new HttpMethodConstraint("POST") });
context.MapRoute(null,
"api/posts/{id}",
new { controller = "Post", action = "Update" },
new { httpConstraint = new HttpMethodConstraint("PUT") });
context.MapRoute(null,
"api/posts/{id}",
new { controller = "Post", action = "Delete" },
new { httpConstraint = new HttpMethodConstraint("DELETE") });
对于使用 ASP.NET MVC 的新手来说,创建匿名对象来处理路由是非常不直观的。我希望看到它被修改成这样(因为我们使用的是 C# 4.0):
context.MapRoute("api/posts",
controller: "Post",
action: "Get",
httpMethodConstraint: HttpMethodConstraint.GET
);
context.MapRoute("api/posts",
controller: "Post",
action: "Insert",
httpMethodConstraint: HttpMethodConstraint.POST
);
context.MapRoute("api/posts/{id}",
controller: "Post",
action: "Update",
httpMethodConstraint: HttpMethodConstraint.PUT
);
context.MapRoute("api/posts/{id}",
controller: "Post",
action: "Delete",
httpMethodConstraint: HttpMethodConstraint.DELETE
);
这也将使它更容易被发现。