地图路由不使用默认操作

map routing not using default action

当我输入 - http://localhost:60559/movies 浏览器正在重定向到 movies/index 为什么?我已经进行了默认操作编辑。所有名称都只是演示。

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

动作是

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));
}

浏览器中的预期结果是 id = 1 但它显示的是 id: 2

我试过你的代码但效果不佳但是 请尝试以下操作,您的代码应该可以正常工作

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

如果你计划添加参数然后使用下面的

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

请添加以下内容

        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 }