4

我想根据特定 json 参数的值将请求路由到不同的操作。

例如,给定以下 json 数据:

{
  type: "type1",
  type1data: "type1value"
}

{
  type: "type2",
  type2data: "type2value"
}

我希望能够对我的 ApiController 进行 2 种不同的操作:

void CreateType1(string type1data) 
{ 
  // ... 
}

void CreateType2(string type2data) 
{ 
  //... 
}

这样的事情怎么能做到?

更新:

如果可能的话,我想要相同的 URL。类似的东西/objects/create

4

2 回答 2

9

我更愿意使用自定义的 ApiControllerActionSelector。

public class MyActionSelector : ApiControllerActionSelector
{
    public override HttpActionDescriptor SelectAction(
                                HttpControllerContext context)
    {
        HttpMessageContent requestContent = new HttpMessageContent(
                                                           context.Request);
        var json = requestContent.HttpRequestMessage.Content
                                .ReadAsStringAsync().Result;
        string type = (string)JObject.Parse(json)["type"];

        var actionMethod = context.ControllerDescriptor.ControllerType
            .GetMethods(BindingFlags.Instance | BindingFlags.Public)
            .FirstOrDefault(m => m.Name == "Create" + type);

        if (actionMethod != null)
        {
            return new ReflectedHttpActionDescriptor(
                               context.ControllerDescriptor, actionMethod);
        }

        return base.SelectAction(context);
    }
}

这是模型。我给它起了一个奇怪的名字,叫做 Abc。

public class Abc
{
    public string Type { get; set; }
    public string Type1Data { get; set; }
}

下面是动作方法。

public void Createtype1(Abc a)
{

}

最后,插入动作选择器。

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );

        config.Services.Replace(typeof(IHttpActionSelector),
                                    new MyActionSelector());
    }
}

如果您现在 POST 到 http://localhost:port/api/yourapicontroller,根据 JSON 中 type 字段中的值,将选择操作方法 Create*。

于 2013-05-14T14:31:24.677 回答
0

像这样的东西。为简单起见,此示例不是正确的异步实现。

using System.Net.Http;
using System.Web.Http;

namespace MyApi.Controllers
{
    public class MyType {
        public string type {get; set;}
    }

    public class MyType1 {
        public string type {get; set;}
        public string type1data {get; set;}
    }

    public class MyType2 {
        public string type {get; set;}
        public string type2data {get; set;}
    }

    public class ObjectsController : ApiController {

        //
        // api/objects/create
        //
        public void Create() {
            // buffer the content to allow the dual reads below
            Request.Content.LoadIntoBufferAsync().Wait();

            // get the body as a object so we can access its type property
            MyType postedObject = Request.Content.ReadAsAsync<MyType>().Result;

            // decide
            switch (postedObject.type) {
                case "type1":
                    CreateType1(Request.Content.ReadAsAsync<MyType1>().Result.type1data);
                    break;
                case "type2":
                    CreateType2(Request.Content.ReadAsAsync<MyType2>().Result.type2data);
                    break;
                default:
                    // 400 Bad Request would be best, I think.
                    break;
            }
        }

        private void CreateType1(string type1data) {
            // ... 
        }

        private void CreateType2(string type2data) {
            // ...
        }
    }
}
于 2013-05-13T18:57:04.477 回答