.Net Core 2.0 WebAPI controller question

envo

Expert Member
Joined
Jan 14, 2014
Messages
3,265
Reaction score
437
*edit to highlight the problem if you don't want to read my explanation*

I have a controller that inserts a user:

Code:
        [HttpPost("InsertUser")]
        [Consumes("application/json")] 
        [ResponseCache(Location = ResponseCacheLocation.None, NoStore = true)]
public IActionResult Insert([FromBody]User user)

GREEN = NOT NULL AND POPULATED WITH DATA

I have another controller that inserts a lead:

Code:
        [HttpPost("InsertLead")]
        [Consumes("application/json")]
        [ResponseCache(Location = ResponseCacheLocation.None, NoStore = true)]
public IActionResult Insert([FromBody]Lead lead)

RED = NULL, NOT PICKING UP DATA

The "Lead" controller was copied from the (working) User controller, however, when I submit the form for the Lead Insert, it doesn't pick up and populate the Lead object like it does for the User one, resulting in a NullReference Exception being thrown when trying to access data within it.

What am I missing here? Why would there be a day/night difference like this? I know we struggled to get the User controller to work as well, trying to figure out [FromBody] and Task<IActionResult> was the culprit (or so we thought)....

I need someone that's way more intelligent than me (and my google skills) to please explain this and what I'm doing wrong here.

Thanks in advance
#buysyouabeer
 
Last edited:
If it can't distinguish which controller to use I am thinking you probably didn't decorate your classes with
e.g.
Code:
[Route("api/[controller]")]
public class Lead

[Route("api/[controller]")]
public class User
 
No, I don't have an issue with the controller or route. It fires the correct one. It routes correctly. I don't have a problem with any of that. I can debug and see how Lead is null but User is not.

Both Lead and User inherits from an object that has a parameterless constructor as well.

The question I have is pretty specific. Let me know if I need to explain it differently.

lol... just noticed your username... you don't have to live up to it ;)
 
Last edited:
lol... just noticed your username... you don't have to live up to it ;)
Your asking for help but you're cocky.

The mistake you are making is the most basic junior dev laziness. If a junior dev asked me that question I would klap them so hard as it means they are too lazy to debug problem.

Your question should be answered:
1. For what obvious reason can it be null?
2. What tool could you use to find the problem?
 
Last edited:
what does your client side look like for those 2 endpoints?
 
Your asking for help but you're cocky.

The mistake you are making is the most basic junior dev laziness. If a junior dev asked me that question I would klap them so hard as it means they are too lazy to debug problem.

Your question should be answered:
1. For what obvious reason can it be null?
2. What tool could you use to find the problem?

**** off won't you. You're the one that suggested the routing didn't work in the first place without reading what I was asking at first.

Turns out the json being received had something weird in it (and by something I mean a UTF8/blank character) so the object mapper didn't map it properly and pushed through a null.
 
what does your client side look like for those 2 endpoints?

Almost exactly the same as the lead form and user insert has similar details (hence why it's inherited from a main object). You were the closest in guessing what could be wrong and where I should focus more on. Thanks
 
**** off won't you. You're the one that suggested the routing didn't work in the first place without reading what I was asking at first.
Look next to your post and see you edited after I posted. If it was that obvious would you had a need to edit it?

The reference to null in your original post was
The "Lead" controller was copied from the (working) User controller, however, when I submit the form for the Lead Insert, it doesn't pick up and populate the Lead object like it does for the User one, resulting in a NullReference Exception being thrown when trying to access data within it.
Any developer would say that is a very weak explanation.

Seems you did follow my advice and used a tool to debug it as you got the problem yourself :laugh:
 
Easiest approach to testing your api I think is using postman or fiddler. It could be you have an issue with the automatic model binder or Your client is appending the data to the url leaving the body empty and the model binder has nothing to bind to your model. With the little information you gave us we have no idea in the pipeline where the information gets lost or if it ever gets to the server for the binding to happen
 
To add onto what others have said; when asking for code assist:
  • Keep in mind we're not able to see your code, so you either need to provide:
    • a code snippet which replicates / illustrates your problem
    • or share your code.
  • Remember nobody is being paid to help you, so be courteous.
 
Top
Sign up to the MyBroadband newsletter
X