地图路由未使用默认操作

时间:2019-06-02 17:16:58

标签: asp.net-mvc model-view-controller maproute

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

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

1 个答案:

答案 0 :(得分:0)

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

        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 }