I have this route that worked before we upgraded our application to MVC3:
routes.MapRoute(
"Versioned API with Controller, ID, subentity",
"api/{version}/{controller}/{id}/{subentity}/",
new { controller = "API", action = "Index" },
new { id = @"\d+", subentity = "serviceitem|accessitem" }
),
I’m attempting to hit this route with a POST to the following url:
/api/1.0/items/3/serviceitem
My controller method has this signature:
[ActionName("Index"), AcceptVerbs(HttpVerbs.Post)]
public ActionResult CreateServiceItem(int id)
When I try and hit this method, I get the following error:
The parameters dictionary contains a null entry for parameter 'id' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.ActionResult CreateServiceItem(Int32)' in 'API.Controllers.ItemsController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter. Parameter name: parameters
Did some sort of syntax change between mvc2 and mvc3?
EDIT: Updated Information!
I think I found the culprit. I’m posting some data that’s JSON data to the URL. My JSON object happens to look like this:
{ Id: null, OtherProperty: 'foo' }
MVC3 is using the Id from my JSON object rather than the ID specified in the URL. Is this behavior configurable?
EDIT 2: Reproducable example:
We use Ext in our application, so my example is with Ext, but I can reproduce it, this is in my /Views/Home/Index.aspx:
Ext.onReady(function() {
var w = new Ext.Window({
title: 'Hi there',
height: 400,
width: 400,
items: [
new Ext.Button({
text: 'Click me',
handler: function() {
var obj = {
Id: null,
Text: 'Hi there'
};
Ext.Ajax.request({
url: '/item/3/serviceitem',
method: 'POST',
jsonData: Ext.util.JSON.encode(obj),
headers: { 'Content-type': 'application/json' },
success: function(result, request) {
console.log(result);
},
failure: function(result, request) {
console.log(result);
}
});
}
})
]
});
w.show();
});
In my Global.asax, I have the following route:
routes.MapRoute(
"Test",
"{controller}/{id}/{subentity}",
new { action = "Index" },
new { id = @"\d+", subentity = "serviceitem" }
);
In my /Controllers/ItemController, I have this method:
[ActionName("Index")]
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult CreateServiceItem(int id)
{
string data;
using (StreamReader sr = new StreamReader(Request.InputStream))
{
data = sr.ReadToEnd();
}
return Json(new
{
DT = DateTime.Now,
Id = id,
PostedData = data
});
}
When I hit the button, causing a POST to the controller w/ the JSON data specified, I get the same error as above. If I don’t send the JSON data, it works (since MVC will use the URL part as the parameter to my method).
Here’s a link to my repro solution zipped up: http://www.mediafire.com/?77881176saodnxp
No, nothing changed in this respect. It’s the kind of questions for which I like to call status no repro.
Steps:
Add
ItemsControllerto the project:Modify the route registration in
Application_Startso that it looks like this:Modify
~/Views/Home/Index.cshtmlview so that it looks like this:Hit F5 to run the project
As expected the default browser launches, an AJAX request is sent to the
CreateServiceItemaction ofItemscontroller which returns the id=3 string and it is this string that gets alerted.Conclusion: either you haven’t shown your actual code or the problem is, as I like to say, in your TV.
UPDATE:
Now that you have posted a reproducible example I see what the problem is and how it is different than my test case. In my test case I performed the request like this:
In your request you are doing this:
There is no longer the model name variable. So there is a conflict when you try to invoke the following controller action:
If the model is not wrapped in a proper object in your request, the model binder picks the value from the POST request first because it has precedence than the value of the url. It’s how the model binder works: it first looks in POSTed data. In ASP.NET MVC 2 this was working fine because there was no
JsonValueProviderFactoryso there was no id parameter in the POST request. In ASP.NET MVC 3 this was introduced and now there is a value for it.The workaround I would suggest you is to wrap your JSON in another object.