如何在Web API中区分具有相同参数的三个POST方法

时间:2013-10-01 07:04:55

标签: asp.net-mvc-4 asp.net-web-api

我的控制器中有三个POST Action方法

  1. PostProjectData([FromBody] MyObject对象)

  2. PostCustData([FromBody] MyObject对象)

  3. PostBookData([FromBody] MyObject对象)

  4. 那么Web API将如何识别每种方法???

    提前致谢

1 个答案:

答案 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。他们应该拥有自己的控制器。是吗?