到达具有相同结构的地址

时间:2019-06-04 04:44:25

标签: c# asp.net-mvc routes

我是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

1 个答案:

答案 0 :(得分: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+" }
);