Reputation: 2594
I've got a really puzzling issue relating to mapping data to a Model in my Web API. It's a bit complex, so let me know if I haven't explained it well enough and I'll do my best to elaborate.
See Edit 2, the problem has changed
I have a MVC 5 application in which the user submits form data and that data is mapped to a model automatically based on matching the names of the fields in the JSON with the names of the model's members. For example, if the incoming JSON for user names is of the form:
{Id=0, Name=testName}
Then there will be a model called (example name only) UsernameModel
that has an int id
and a string name
among other properties that don't need to be mapped from JSON (there are a number of these models for a number of different forms).
I'm not sure how useful it will be but I noticed that the call at the bottom of the stack trace was
System.Web.Http.Validation.DefaultBodyModelValidator.ValidateNodeAndChildren(
ModelMetadata metadata, ValidationContext validationContext, Object container,
IEnumerable`1 validators)
Could this Validation
have something to do with my probleM?
None of us have any idea as to why this is happening, and only happening on my machine. Any ideas/suggestions? Things to try? Ways to narrow down the error?
Edit: Found another question which may be related, but it's really odd that this only occurs on my computer, whereas in that post it was just a general problem.
Edit 2: I've narrowed down the issue, and have a much more specific problem now. I have discovered that on my computer the Web API's mapping of the JSON to the C# object (or some other process called during the mapping) calls the get()
for each property in the process of mapping the object. This is why the exception is being thrown; one of the properties throws an exception when the get()
is called (by design). On others' computers, the get()
is called only on those properties that are mapped to by the JSON, and we're not sure why.
Edit 3 (revised): In researching System.Web.Http.Validation
I found this post which asked how to disable it. I followed the instructions in the accepted answer and disabled the default validation, and my issue went away! I'm still left with the question as to why this would be different between two computers, but at least I'm getting somewhere!
Upvotes: 2
Views: 357
Reputation: 2594
I was able to solve my problem by disabling the default System.Web.HTTP.Validation
validation on my entire project. See this stack post for where I got the solution, which is shown below:
config.Services.Clear(typeof(ModelValidatorProvider));
If this is put at the top of the RouteConfig
file, the default validation will be disabled, and the issue does not occur. This still does not explain why it was only validating that property on my computer, and that question remains unanswered, however for the purpose of this question this solves the problem.
Upvotes: 0
Reputation: 29252
Check the break on exceptions settings in your Visual Studio and the other users'. Maybe it is throwing the same exception on their computers but their Visual Studio is ignoring it because it's handled. That would be an environmental difference that has nothing to do with the code or configuration.
Upvotes: 0
Reputation: 29252
For the same application code to behave so differently on two computers is unlikely. It's not impossible, just unlikely.
When something happens that borders on impossible, question every detail. In this case, question whether you're actually executing the same code in both places. Make sure one of you doesn't have an outdated version of the code from source control. Check the versions of the libraries you're referencing. See if someone has a different web.config. If necessary, step through the code in both places.
I don't know what it is, but I'm 90% certain you're going to find that you're getting different behaviors because you're executing something slightly different.
Upvotes: 1