Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6713465
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T08:23:02+00:00 2026-05-26T08:23:02+00:00

I have this route that worked before we upgraded our application to MVC3: routes.MapRoute(

  • 0

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
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T08:23:02+00:00Added an answer on May 26, 2026 at 8:23 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following URl: http://localhost:12981/BaseEvent/EventOverview/12?type=Film This is route: routes.MapRoute( Default, // Route name
I have a route that is suppose to match something like this localhost:3000/site/admin and
I have a route which routes a request to somepath/:id(.:format) to somecontroller#show . This
Short: Is there a way to have a route-definition that will pass the CONTROLLER/ACTION
I have routes like this: map.namespace 'prepayments', :path_prefix => '/:locale/prepayments' do |prepayment| prepayment.root :controller
I'm using Expressjs w/ node.js and I have a route like this: users.init(app.db, function()
I have a route defined like this: GET /question/:q_id controllers.Questions.viewQuestion(q_id: Long) Then in my
ok i got this problem. i have this routes: (code bit change) File:/home/dotcloud/current/config/routes.js exports.routes
In my routes.rb I have this: map.namespace :admin do |admin| admin.resources :galleries do |galleries|
I have a router like this, as main entry point: window.AppRouter = Backbone.Router.extend({ routes:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.