我的控制器中有三个POST Action方法
PostProjectData([FromBody] MyObject对象)
PostCustData([FromBody] MyObject对象)
PostBookData([FromBody] MyObject对象)
那么Web API将如何识别每种方法???
提前致谢
答案 0 :(得分:2)
您认为在Web API平台中这是一个很好的设计吗?
我认为最好有
ProjectController将负责PostProjectData([FromBody] MyObject对象)
CustController将负责PostCustData([FromBody] MyObject对象)
BookController将负责PostBookData([FromBody] MyObject对象)
如果您真的想这样,可以通过以下方式设置路由。
config.Routes.MapHttpRoute(
string.Empty,
"yourroute1",
new { controller = "yourcontroller", action = "PostProjectData" },
new { httpMethod = new HttpMethodConstraint(HttpMethod.Get.Post) }
);
config.Routes.MapHttpRoute(
string.Empty,
"yourroute2",
new { controller = "yourcontroller", action = "PostCustData" },
new { httpMethod = new HttpMethodConstraint(HttpMethod.Get.Post) }
);
config.Routes.MapHttpRoute(
string.Empty,
"yourroute3",
new { controller = "yourcontroller", action = "PostBookData" },
new { httpMethod = new HttpMethodConstraint(HttpMethod.Get.Post) }
);
但我认为你应该重新设计控制器,因为资源是Project,Cust和Book。他们应该拥有自己的控制器。是吗?