I can not understand the principle of operation of the id argument on MVC, since when I create an action

public class MovieController : Controller { public ActionResult act(int movieid) { return Content("movieid = " + movieid); } } 

and refer to http: // localhost: 49721 / Movie / act / 1 an error is issued. On the Internet found such information. "In the marshuritazotor, the argument is not a movieid, but an id, because of this, the argument must be an id." But I can not understand, if we pass a number, then what's the difference in the name of the argument in action? We still pass the integer to the router

    2 answers 2

    When a request comes in for a URL like "Movie / act / 1", ASP.NET pulls out three collections of named parameters from it:

    1. GET parameters: they are not in this request, empty collection;
    2. POST parameters: there are none in this request either, the collection is empty again;
    3. Route Parameters - here they are. If you have a default routing setup, then these parameters will be as follows:
      • controller = "Movie"
      • action = "act"
      • id = "1"

    The first two parameters are used to find the method to call. Then comes the phase of binding the parameters of the method. And here ASP.NET by default uses parameter names !

    Your parameter has the name movieid - and for it there were no values ​​in the collection of input parameters.

    Ways to fix:

    1. correct method parameter name:

       public ActionResult act(int id) 
    2. add a separate route for your action:

       routes.MapRoute("MovieAct", "Movie/act/{movieid}", new { controller = "Movie", action = "act" }); 
    3. do the same thing - but through the attribute:

       routes.MapMvcAttributeRoutes(); // ... [Route("Movie/act/{movieid}")] public ActionResult act(int movieid) 
    4. change bind (works only in ASP.NET Core):

       public ActionResult act([FromRoute(Name = "id")]int movieid) 

      In your Global.asax.cs there is such code:

        routes.MapRoute( "Default", // Route name "{controller}/{action}/{id}", // URL with parameters new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults ); 

      In the absence of other registered routes, the parameter going through the slash after the name of the action method is called id .

      You can write your ModelBinder for this method and name the parameter as you like.

      • I know that, but still we transfer the finished number there. For example, 3. What is the difference how this variable is called if we transfer the finished number? - user265018 pm
      • @ShamilQurbanli What is the "ready number" in your understanding, and how does it differ from the "unprepared"? - Igor
      • Well, if we wrote void func (int id) and then we call the function like this: movie movie = 3; funk (movieid) means passing a number. The compiler does not give an error. Accordingly, when we create a public ActionResult foo (int movieid) and write localhost: 49721 / Movie / act / 3, then we have id = 3 and the result is movieid = 3. Is not it ? We pass the value 3, what's the difference as a variable? - user265018
      • 2
        @ShamilQurbanli Yes, that’s not true. The binding of values ​​from the URL and other elements of the query occurs by parameter names. docs.microsoft.com/en-us/aspnet/core/mvc/models/model-binding - Igor