ASP.NET MVC: as a distingush controller between parameters in a URL and POST is sent

I would like to better understand how the controller method knows when the returned parameter should be extracted from the mail data or URL.

Take the following example:

URL: /ModelController/Method/itemID 
// Where itemID is the id (int) of the item in the database
POST: objectOrArray: {JSON Object/Array}

The controller will look something like this:

[HttpPost]
public ActionResult InputResources(int? id, Object objectOrArray)

Now, somehow the method is smart enough to look for the first parameter id,, in the site URL and Objectin HTTPPost.

While this works, I don’t know why, and as a result, I sometimes encounter unpredictable and erratic behavior. For example, I seem to have found (although I'm not 100% sure) that removing ?from int? idmakes the controller method immediately assume that it should look for the identifier in HTTPPost, not the URL.

So, I would like to clarify the following points:

? ( [HttpPost] ?)

? (, ? id ?)

, , inpact? (.. Object id)

, , , , .

+5
1

Global.asax:

routes.MapRoute(
    "Default",                                            
    "{controller}/{action}/{id}",                     
    new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);

, MVC , POST. , , .

:

  • ,
  • (Request.Form)
  • JSON (Request.InputStream), , AJAX
  • (RouteData.Values)
  • Querystring (Request.QueryString)
  • (Request.Files)

, , , POST . , MVC nullable POST, , RouteData, . -nullable, POST, , RouteData, .

: http://msdn.microsoft.com/en-us/magazine/hh781022.aspx

+5

All Articles