我们正在尝试在API上实现VersionedRoutes,并在类中声明我们的路由:
new VersionedRoute(
version,
"GetRiskReports",
"/Members/{id}/Cpin/RiskReports",
new RouteValueDictionary
{
{"controller", "Members"},
{"action", "GetRiskReports"},
{"id", RouteParameter.Optional}
}),
new VersionedRoute(
version,
"PostRiskReports",
"/Members/{id}/Cpin/RiskReports",
new RouteValueDictionary
{
{"controller", "Members"},
{"action", "PostRiskReports"},
{"id", RouteParameter.Optional}
})
控制器方法声明如下:
[HttpGet]
public HttpResponseMessage GetRiskReports(int id)
[HttpPost]
public HttpResponseMessage PostRiskReports(int id)
显然此时,对url / Members / 123 / Cpin / RiskReports的GET请求路由到第一个路由ok,但是POST请求无法路由,因为它匹配第一个路由但是控制器方法被标记仅作为GET。无论如何都要将HTTP方法添加到自定义路由。客户端不想更改路由名称。
答案 0 :(得分:1)
在 RouteConfig.cs 中,注册此配置:
config.Routes.MapHttpRoute("GetRiskReports", "Members/{id}/Cpin/RiskReports" ,
new {
controller = "Members",
action = "GetRiskReports"
},
new {
HttpMethod = new HttpMethodConstraint(HttpMethod.Get)
}
);
config.Routes.MapHttpRoute("PostRiskReports", "Members/{id}/Cpin/RiskReports" ,
new {
controller = "Members",
action = "PostRiskReports"
},
new {
HttpMethod = new HttpMethodConstraint(HttpMethod.Post)
}
);