0

i have following APi Controller

public class TriggerController : ApiController
    {
        [HttpPost]
        [ActionName("fileimport")]
        public int CreateFileImportTrigger([FromBody]ITrigger _object)
        {
            return scheduler.CreateFileImportTrigger(_object);
        }
.....
...

My WEbApiConfig is like so and the call matches the third route.

  public static void Register(HttpConfiguration config)
        {

        config.Routes.MapHttpRoute(
        name: "ApiById",
        routeTemplate: "api/{controller}/{id}",
        defaults: new { id = RouteParameter.Optional },
        constraints: new { id = @"^[0-9]+$" }
        );





config.Routes.MapHttpRoute(
            name: "ApiByName",
        routeTemplate: "api/{controller}/{action}/{name}",
        defaults: null,
        constraints: new { name = @"^[a-z]+$" }
    );

    config.Routes.MapHttpRoute(
        name: "ApiByAction",
        routeTemplate: "api/{controller}/{action}",
        defaults: new { _object = RouteParameter.Optional }
    );

    var appXmlType = config.Formatters.XmlFormatter.SupportedMediaTypes.FirstOrDefault(t => t.MediaType == "application/xml");
    config.Formatters.XmlFormatter.SupportedMediaTypes.Remove(appXmlType);

}

The ITrigger is defined as so.

public interface ITrigger
{
    string Name{ get; set; }
    string FilePath{ get; set; }
    string TriggeredJobEntityCode{ get; set; }
    int ID{ get; set; }
    TriggerTypes Type{ get; set; }
    string SubmitDate{ get; set; }
    DateTime SubmitTime{ get; set; }
    IJob[] Jobs { get; set; }

}

and

I have specified in the Http Header the Content-Type as application/json

the body i am posting is like so

{
    "Name": "FileEventTrigger",
    "FilePath": "C:\\Users\\file.txt",
    "TriggeredJobEntityCode": null,
    "Jobs": [
        {
            "ID": 1643,
            "TemplateID": 1643,
            "Name": "SomeJob",
            "SubmitToRunTime": "0001-01-01T00:00:00",
            "Parameters": [
                {
                    "Name": "Seconds",
                    "Value": "30",
                    "DefaultValue": "30",
                    "isRequired": true,
                    "DataType": "Integer"
                }
            ]
        }
    ],
    "ID": 1,
    "Type": 0,
    "SubmitDate": "7/21/2013",
    "SubmitTime": "0001-01-01T00:00:00"
}

I have a break point at the beginnning of the CreateFileImportTrigger method where i check the value of the _object and it shows up "null" after i post the call. So somewhere the WebApi application is not reading the body and serializing it to _object parameter. The service is returning ITrigger object fine only is not able to handle POST.

Any help would be appreciated.

Thanks

dpradhan
  • 1
  • 2
  • Upon further investigation, when i change the parameter of my method from Interface to Concrete then it appears to fill up properly (only none Interface properties). Is there a way to keep the parameter as Interface rather than a concrete class and still make it work ? I did find a post similar to my situation but I cannot understand that answer http://stackoverflow.com/questions/17119073/asp-net-web-api-operation-with-interfaces-instead-concrete-class – dpradhan Jul 26 '13 at 21:44
  • How is Json.NET supposed to decide what concrete type to deserialize if you don't specify a concrete parameter type? The only way this might work is if you turn on type name handling. – Youssef Moussaoui Jul 26 '13 at 22:41
  • Thanks Youssef, How would I turn on type name handling? Is it using ModelBinderProvider and ModelBinder? – dpradhan Jul 26 '13 at 23:31
  • Also how is Json.Net deserializing the Interface object without having to specify what type its converting it to, since my GET requests are returning fine. – dpradhan Jul 26 '13 at 23:46
  • GET requests only exercise serialization, not deserialization - you're returning a concrete implementation of the interface that Json.NET uses. – Youssef Moussaoui Jul 27 '13 at 01:17
  • Thanks, I ended up changing my design and used concrete objects instead of interface. – dpradhan Jul 30 '13 at 18:05

0 Answers0