2

I have the following json Object which i am sending to Web Api controller Using ajax.

var dataObject = {
                    LanguageId: 1,
                    SubTenantId: 1,
                    Object:{"TestApplication":{"Aplication":null}} 
                }

Controller function RequestObject generic class.

 public IHttpActionResult ComplexObject(RequestObject<TestApplicationMain> pubRequestObject)

I am using following Class hierarchy.

public class TestApplicationMain
{
    public TestApplicationMain()
    {
        TestApplication = new TestApplication();
    }
 public TestApplication TestApplication{get; set;}

}

public class TestApplication
{
    public TestApplication()
    {
        Aplication = new List<ApplicationSearchParam>();
    }
    public List<ApplicationSearchParam> Aplication { get; set; }

}

public class ApplicationSearchParam
{
    public ApplicationSearch ApplicationSearch { get; set; }
    public string OrderBy { get; set; }
}

When i send {"TestApplication":{"Aplication":null}} json to the controller. I receive One item Aplication .

Api controller works as expected when i send {"TestApplication":{"Aplication":undefined}} OR {"TestApplication":{"Aplication":[]}}

My Question is Why Asp.net WebApi Controller add one item in nested child list when its set to null ?.

Muhammad Nasir
  • 2,126
  • 4
  • 35
  • 63
  • 1
    What is the item inside `Aplication` ? When I test your code, what I've seen for `Aplication` is `List` with 0 count. And this is normal beacuse, you are instantiating in constructor. – ibubi Jul 26 '17 at 12:05
  • 4
    Include `RequestObject` in the [mcve] – Nkosi Jul 26 '17 at 12:17
  • ApplicationSerachParam is a class having a number of. with Appliacation:null i receive one List with 1 count – Muhammad Nasir Jul 27 '17 at 04:24
  • i think you missing something in your code can you post the rest of the controller and models? – Liran Jul 30 '17 at 15:57
  • I would suggest you work with the Framework as you have evaluated. In my experience, always pass a value (non-NULL) to Web API, so that it will properly map to properties. Also, best to accept a JSON object into your Web API and try not to Auto-Map the JSON to .Net Object. You are adding many layers of obscurity that will be hard to track-down. – GoldBishop Jul 31 '17 at 13:06

1 Answers1

1

First Problem Spotted:

var dataObject = {
                    LanguageId: 1,
                    SubTenantId: 1,
                    Object:{"TestApplication":{"Aplication":null}} 
                }
  1. Your JSON is at a level higher than TestApplicationMain
  2. You defined LanguageId and SubTenantId but your receiving class TestApplicationMain does not handle either of the values.
  3. You have not defined Object in TestApplicationMain

The reason your first test scenario {"TestApplication":{"Aplication":null}} works is because TestApplication is a property of TestApplicationmain

When you are automapping JSon Key-Value pairs, the name of the Key needs to match the name of the Property you wish its value to be assigned.

You test JSON should be:

var dataObject = {"TestApplication":{"Aplication":""}}

as this structure would map to:

TestApplicationMain -> TestApplication -> Aplication = ""

My Suggestions:

GoldBishop
  • 2,820
  • 4
  • 47
  • 82
  • I new this work with [] undefined and "" but i want to make work with null – Muhammad Nasir Jul 31 '17 at 15:40
  • then wrap NULL in text `"null"` or handle the NULL value in Web API...be prepared for unhandle situations when relying on automapping. I accept JSON as a string and parse using JsonSerializer object and manually map the fields I am expecting to the class/properties. – GoldBishop Jul 31 '17 at 15:45