title: ASP.Net MVC RouteAttribute Error with Route Parameter
body: <p>I'm getting a lot of different errors while trying to add Route mapping to a certain action!</p>
  
  <p>I'm trying to get this route for <code>GET /Admin/Users/User/1</code>
  and <code>POST /Admin/Users/User</code></p>
  
  <p>But sadly there is already a User property in Controller!
  So i cannot use <code>public ActionResult User(long id)</code> because i need to hide the user property (that i need to keep because it's the IPrincipal of the Controller and i still get the same error).</p>
  
  <p>Defining route this way :</p>
  
  <pre><code>// First in RouteConfig
  routes.MapMvcAttributeRoutes();
  
  routes.MapRoute(
      name: "Default",
      url: "{controller}/{action}/{id}",
      defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
  );
  
  // Then i'm registering my Areas.
  </code></pre>
  
  <p>this controller is in Admin Areas.
  UsersController : Controller</p>
  
  <pre><code>[HttpGet]
  [Route(Name = "User/{id:long}")]
  public ActionResult GetUser(long id)
  {
      var model = new UserViewModel
      {
          User = _usersService.GetUser(id),
          Roles = _rolesService.GetRoleDropdown()
      };
  
      return View("User");
  }
  
  [HttpPost]
  [Route(Name = "User")]
  public ActionResult GetUser(UserViewModel model)
  {
      if (ModelState.IsValid)
      {
          _usersService.UpdateUserRoles(model.User);
          return RedirectToAction("Index");
      }
  
      return View("User", model);
  }
  </code></pre>
  
  <p>Here is the error i'm getting :</p>
  
  <pre><code>The parameters dictionary contains a null entry for parameter 'id' of non-nullable type 'System.Int64' for method 'System.Web.Mvc.ActionResult User(Int64)' in 'MyProjectName.Web.Areas.Admin.Controllers.UsersController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter. Parameter name: parameters
  </code></pre>
  
  <p>I'm not sure to really understand what's wrong!</p>
  
  <p>I checked this page that explain the attributes and i see nothing wrong 
  <a href="https://blogs.msdn.microsoft.com/webdev/2013/10/17/attribute-routing-in-asp-net-mvc-5/" rel="nofollow">https://blogs.msdn.microsoft.com/webdev/2013/10/17/attribute-routing-in-asp-net-mvc-5/</a></p>
  
  <h2>EDIT 1</h2>
  
  <p>It still doesn't work, i changed my registration to </p>
  
  <pre><code>routes.MapMvcAttributeRoutes();
  
  AreaRegistration.RegisterAllAreas();
  
  routes.MapRoute(
      name: "Default",
      url: "{controller}/{action}/{id}",
      defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
  );
  </code></pre>
  
  <p>Here is my full controller code because now it doesn't work for my Index action that was not in the initial code.</p>
  
  <pre><code>[RouteArea("Admin")]
  [RoutePrefix("Users")]
  public class UsersController : Controller
  {
      private readonly IUsersService _usersService;
      private readonly IRolesService _rolesService;
  
      public UsersController(
          IUsersService usersService,
          IRolesService rolesService)
      {
          _usersService = usersService;
          _rolesService = rolesService;
      }
  
      [HttpGet]
      [Route(Name = "Index")]
      public ActionResult Index()
      {
          var model = new UsersViewModel
          {
              Users = _usersService.GetUsers()
          };
  
          return View(model);
      }
  
      [HttpGet]
      [Route(Name = "User/{id:long}")]
      public new ActionResult User(long id)
      {
          var model = new UserViewModel
          {
              User = _usersService.GetUser(id),
              Roles = _rolesService.GetRoleDropdown()
          };
  
          return View("User");
      }
  
      [HttpPost]
      [Route(Name = "User")]
      public new ActionResult User(UserViewModel model)
      {
          if (ModelState.IsValid)
          {
              _usersService.UpdateUserRoles(model.User);
              return RedirectToAction("Index");
          }
  
          return View("User", model);
      }
  }
  </code></pre>
  
  <p>Now, i'm getting while trying to go to my index action:</p>
  
  <blockquote>
    <p>The current request is ambiguous between the following action methods:
    System.Web.Mvc.ActionResult Index() on type
    EspaceBiere.Web.Areas.Admin.Controllers.UsersController
    System.Web.Mvc.ActionResult User(Int64) on type
    EspaceBiere.Web.Areas.Admin.Controllers.UsersController
    System.Web.Mvc.ActionResult
    User(EspaceBiere.Web.Areas.Admin.ViewModels.Users.UserViewModel) on
    type EspaceBiere.Web.Areas.Admin.Controllers.UsersController</p>
  </blockquote>
  
  <p>and this while trying to go to User Action</p>
  
  <blockquote>
    <p>A public action method 'User' was not found on controller
    'EspaceBiere.Web.Areas.Admin.Controllers.UsersController'.</p>
  </blockquote>
  
  <p>And my link to the index action was :</p>
  
  <p>tried with   <code>/Admin/Users</code>   for my <code>Index</code> Action</p>
  
  <p>tried with   <code>/Admin/Users/User/1</code>   for <code>User</code> Action</p>
  
  <h2>EDIT 2</h2>
  
  <p>Ok, my index is now working perfectly, but my user action still is not working!
  I've removed all the Name properties of the RouteAttribute to keep them in the constructor (as template)  ->         [Route("User/{id:long}")]</p>
  
  <p>Sorry, if i didn't see them on first read!</p>
  
  <p>Here is the link to the action</p>
  
  <pre><code>                    &lt;a href="@Url.Action("User", "Users", new { Area = "Admin", id = user.UserId })" class="btn btn-warning"&gt;
                      &lt;i class="fa fa-pencil"&gt;&lt;/i&gt;
                  &lt;/a&gt;
  </code></pre>
  
  <p>Here is the error</p>
  
  <pre><code>No matching action was found on controller 'EspaceBiere.Web.Areas.Admin.Controllers.UsersController'. This can happen when a controller uses RouteAttribute for routing, but no action on that controller matches the request.
  </code></pre>
  
  <p>It does work if i write in the URL   /Admin/Users/User/1
  So how should i write my Url.Action ?</p>