asp.net-mvcmodel-view-controllermaproute

map routing not using default action


when I enter - http://localhost:60559/movies The browser is redirecting to movies/index Why? I have made the default action edit. All the names are just demo.

routes.MapRoute(
  "searchByName",
  "Movies/edit",
  new {Controller = "Movies", action = "edit"}
);

actions are

public ActionResult index(int? id) {
   if(!id.HasValue)
      id = 2;
   return Content("id: " + id);
}
public ActionResult edit(int? id) {
   if (!id.HasValue)
     id = 1;
   return Content(String.Format("id =  {0}", id));
}

the expected result is id = 1 in the browser but it is showing id: 2


Solution

  • Ive tried your code and it didnt work well but Please try the following and you code should work just fine

            routes.MapRoute(
               name:"searchByName",
               url: "{controller}/{action}",
               defaults: new { Controller = "Movies", action = "edit" }
          );
    

    if you plane to add parameters then use following

            routes.MapRoute(
               name:"searchByName",
               url: "{controller}/{action}/{id}",
               defaults: new { Controller = "Movies", action = "edit", id = UrlParameter.Optional }
          );
    

    Please Add the following

            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
    
            routes.MapRoute(
                   name: "SearchByName",
                   url: "Movies/{action}",
               defaults: new { Controller = "Movies", action = "edit" }
               );
    
            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }