【问题标题】:Asp.Net MVC Routing ConceptAsp.Net MVC 路由概念
【发布时间】:2020-08-06 06:00:32
【问题描述】:

我在处理 MVC 中的路由时遇到问题。

我在 Route.Config 中定义了以下路由

  routes.MapRoute(
            name: "Test",
            url: "{controller}/{action}/{param}",
            defaults: new { controller = "Home", action = "FirstAction" }
        );
        routes.MapRoute(
            name: "Testy",
            url: "{controller}/{action}/{secondparm}",
            defaults: new { controller = "Home", action = "SecondAction" }
        );

        routes.MapRoute(
          name: "Test2",
          url: "{controller}/{action}/{encodedparam}",
          defaults: new { controller = "User", action = "UserInfo" }
      );

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );

问题是第一条路线工作正常,但在第二条和第三条路线中,我得到了相应参数的空值。

我错过了什么吗?

提前致谢

【问题讨论】:

    标签: c# asp.net-mvc routes


    【解决方案1】:

    您必须匹配第三个参数名称。如果你写:

    {id}

    你必须写

    public ActionResult AnyAction(int id)

    id 可以是任何类型

    我认为您的路线之间存在冲突。你只需要这个映射:

    routes.MapRoute(
                "Default",                                              // Route name
                "{controller}/{action}/{id}",                           // URL with parameters
                new { controller = "Home", action = "Index", id = UrlParameter.Optional }  // Parameter defaults
            );
    

    如果您需要为其他元素自定义路由,请不要将"{controller}/{action}/..." 放在里面,因为他会采用第一个匹配的路由配置。 如果参数名称与“id”不同,您可以调用所有路由精确的参数名称:

    http://localhost/home/firstaction?param=123
    http://localhost/home/secondaction?secondparam=123
    http://localhost/user/userinfo?encodedparam=123
    

    【讨论】:

    • 感谢您的回复,但我没有得到答复。
    • 你能给我们你的代码对应的控制器动作吗?
    • 你确定,public ActionResult SecondAction(string secondparm) { return View(); } public ActionResult FirstAction(string param) { ViewBag.Message = "我的页面。";返回视图(“联系人”); }
    • localhost:portnumber/home/secondaction/value --> 无法在操作方法localhost:portnumber/home/firstaction/value 中获取参数值 --> 这工作正常,我在操作方法localhost:portnumber/user/userinfo/value 中获得了参数值 --> 无法获取action方法中的参数值
    • Hii @Orkad 我已经查看了你的回复,看起来很酷,你能解释一下这种路由行为的原因吗?
    【解决方案2】:

    因为您想以这种方式使用 URL:localhost:portnumber/home/secondaction/value 而不是 localhost:portnumber/home/secondaction?secondparam=value。

    您必须遵循以下方法,对于您的所有操作方法,无论它们是否包含参数,该方法的行为都是通用的。

    • 在RouterConfig.cs中声明默认路由为:
    routes.MapRoute(
        name: "Default",
        url: "{controller}/{action}/{id}",
        defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
    );
    
    • 那么方法中的参数名称必须与默认路由中的相同:
    public ActionResult SecondAction(string id)
    { 
        return View(); 
    }
    
    • 参数类型可以是任何类型。

    这样就不需要声明其他路由了。

    【讨论】:

    • @Aman 有帮助吗?你的问题解决了吗?
    猜你喜欢
    • 1970-01-01
    • 2016-09-03
    • 2017-04-11
    • 1970-01-01
    • 2020-09-13
    • 1970-01-01
    • 2011-03-28
    • 1970-01-01
    相关资源
    最近更新 更多