2

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

1

1 Answer 1

4

No, nothing changed in this respect. It's the kind of questions for which I like to call status no repro.

Steps:

  1. Create a new ASP.NET MVC 3 project using the default template
  2. Add ItemsController to the project:

    public class ItemsController : Controller
    {
        [ActionName("Index")]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult CreateServiceItem(int id)
        {
            return Content(id.ToString());
        }
    }
    
  3. Modify the route registration in Application_Start so that it looks like this:

    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
    
        routes.MapRoute(
          "Versioned API with Controller, ID, subentity",
          "api/{version}/{controller}/{id}/{subentity}",
          new { controller = "API", action = "Index" },
          new { id = @"\d+", subentity = "serviceitem|accessitem" }
        );
    
        routes.MapRoute(
            "Default",
            "{controller}/{action}/{id}",
            new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );
    }
    
  4. Modify ~/Views/Home/Index.cshtml view so that it looks like this:

    <script type="text/javascript">
        $.ajax({
            url: '/api/1.0/items/3/serviceitem',
            type: 'POST',
            success: function (result) {
                alert(result);
            }
        });
    </script>
    
  5. Hit F5 to run the project

  6. As expected the default browser launches, an AJAX request is sent to the CreateServiceItem action of Items controller 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:

$.ajax({
    url: 'item/3/serviceitem',
    type: 'POST',
    contentType: 'application/json',
    data: JSON.stringify({ model: { id: null, text: 'Hi there' } }),
    success: function (result) {
        console.log(result);
    }
});

In your request you are doing this:

$.ajax({
    url: 'item/3/serviceitem',
    type: 'POST',
    contentType: 'application/json',
    data: JSON.stringify({ id: null, text: 'Hi there' }),
    success: function (result) {
        console.log(result);
    }
});

There is no longer the model name variable. So there is a conflict when you try to invoke the following controller action:

public ActionResult CreateServiceItem(int id)

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 JsonValueProviderFactory so 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.

22
  • What if you post some JSONData along with your request?
    – Jonas
    Oct 21, 2011 at 20:44
  • @Jonas, ahhh, now you start talking about some JSON data. That kind of changes what you asked initially. You should have shown your code. Anyway, still works perfectly fine for me. Request $.ajax({ url: '/api/1.0/items/3/serviceitem', type: 'POST', data: JSON.stringify({ model: { foo: 'bar'} }), contentType: 'application/json', success: function (result) { alert(result); } }); and controller action signature: public ActionResult CreateServiceItem(int id, MyViewModel model) with a view model: public class MyViewModel { public string Foo { get; set; } } => no problem whatsoever. Oct 21, 2011 at 20:49
  • What happens if your JSON data contains a property called 'ID'?
    – Jonas
    Oct 21, 2011 at 20:51
  • @Jonas, still works perfectly fine for me. The id action parameter gets assigned 3 from the route and my view model Id property gets assigned the whatever value I give it in my JSON.stringify method. So really, if you have further questions, please show your code and explain your problems because as I said: status no repro. Oct 21, 2011 at 20:54
  • but if your JSON data looks like this: {Id: null} does it still work?
    – Jonas
    Oct 21, 2011 at 20:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.