Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I'd much rather use a custom ApiControllerActionSelector.</p> <pre><code>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 =&gt; m.Name == "Create" + type); if (actionMethod != null) { return new ReflectedHttpActionDescriptor( context.ControllerDescriptor, actionMethod); } return base.SelectAction(context); } } </code></pre> <p>Here is the model. I gave it a weird name of Abc.</p> <pre><code>public class Abc { public string Type { get; set; } public string Type1Data { get; set; } } </code></pre> <p>Here is the action method.</p> <pre><code>public void Createtype1(Abc a) { } </code></pre> <p>Finally, plug-in the action selector.</p> <pre><code>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()); } } </code></pre> <p>If you now POST to http://localhost:port/api/yourapicontroller, depending on the value in type field in JSON, the action method Create* will be selected.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload