代码之家  ›  专栏  ›  技术社区  ›  Davide Vitali

路由配置不接受参数

  •  0
  • Davide Vitali  · 技术社区  · 5 年前

    我在控制器MattinalEcontroller中有这个动作:

    public ActionResult Modifica(int id)
    {
        // manipulate data from repository
    
        return RedirectToAction("Modifica", "Modifica");
    }
    

    这是我的路线:

    routes.MapRoute(
        name: "",
        url: "",
        defaults: new { controller = "Mattinale", action = "Index" }
        );
    
    routes.MapRoute(
        name: "",
        url: "ModificaComunicazione/{IDArticolazione}",
        defaults: new { controller = "Mattinale", action = "Modifica" }
        );
    
    routes.MapRoute(
        name: "",
        url: "{anno}/{mese}/{giorno}",
        defaults: new { controller = "Mattinale", action = "Index" }
        );
    
    routes.MapRoute(
        name: "Default",
        url: "{controller}/{action}/{id}",
        defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
    );
    

    当我开始调试并尝试调用路由时 myHost/ModificaComunicazione/5 (此处为随机数),我得到此错误(已翻译):

    参数字典包含非参数“id”的空值 方法“System.Web.MVC.ActionResult”的可为空的类型“System.Int32” modifica(int32)'在'mattinale.webui.controllers.mattinalecontroller'中'

    看起来它不接受参数。我错过了什么?!

    谢谢,Davide。

    2 回复  |  直到 5 年前
        1
  •  1
  •   Hardik Dhankecha    5 年前

    只需浏览这个链接。 Route config doesn't take the parameter . 参数名称应与操作中定义的名称相同。

        2
  •  0
  •   Seyhmus Gokcen    5 年前

    戴维德,是的,你从URL中得到ID参数,但不发送到重定向。您应该将ID参数发送到RedirectToAction方法。 退房: RedirectToAction with parameter