【问题标题】:route to adresses with same structure路由到具有相同结构的地址
【发布时间】:2019-06-04 04:44:25
【问题描述】:

我是 MVC 和 web 开发的新手,我有以下路线:

        //"/display/flight1/4" - upload frm flight1 file, show as animation(4 times in a sec), show end alert
        routes.MapRoute(
            name: "uploadPlaneData",
            url: "display/{fileName}/{times}",
            defaults: new { controller = "Display", action = "UploadPlaneData", fileName = "flight1", times = 4 }
        );

        //"/display/127.0.0.1/5400" - check plane place(port 5400 ip 127.0.0.1)(lat, lon) and show a plane icon on the map
        routes.MapRoute(
            name: "showPlaneIcon",
            url: "display/{ip}/{port}",
            defaults: new { controller = "Display", action = "ShowPlaneIcon", ip = "127.0.0.1", port = 5402 }
        );

问题是,如果我试图导航到第二页,它会显示第一页。我将第二个放在代码中的第一个之上,然后导航到一个顶部,我的问题是如何导航到 /display/127.0.0.1/5400/display/flight1/4

【问题讨论】:

  • 抱歉没能给出好的答案。我通常使用属性路由,所以我对 MVC 路由不是很熟悉。我错误地假设它是一样的。
  • @John 我和你提供的链接一样,(我在两条路线中都添加了正则表达式),现在它可以工作了,谢谢你的帮助
  • 如果你添加一个答案,我会支持它:)

标签: c# asp.net-mvc routes


【解决方案1】:

您可以尝试将regular expressions 用于两条路由中的属性。例如:

routes.MapRoute(
    name: "uploadPlaneData",
    url: "display/{fileName}/{times}",
    defaults: new { controller = "Display", action = "UploadPlaneData", fileName = "flight1", times = 4 },
    constraints: new { fileName = @"\w+", times = @"\d+" }
);

routes.MapRoute(
    name: "showPlaneIcon",
    url: "display/{ip}/{port}",
    defaults: new { controller = "Display", action = "ShowPlaneIcon", ip = "127.0.0.1", port = 5402 },
    constraints: new { ip = @"\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b", port = @"\d+" }
);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-06-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-22
    • 2015-08-29
    • 1970-01-01
    相关资源
    最近更新 更多