Jonas
Jonas

Reputation: 4584

Route broken under MVC3

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

Upvotes: 2

Views: 719

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1038720

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.

Upvotes: 4

Related Questions