使用类似的MapRoutes重写URL

时间:2014-02-10 11:43:12

标签: asp.net asp.net-mvc url-rewriting url-routing maproute

我有2个类似的maproute请求,但我正在尝试定位不同的路由。

基本上我正在使用ASP.NET MVC创建一个图片项目。

我想要的是将URL设为:

website.com/pictures/username

website.com/pictures/pictureid

我正在使用它作为地图路线atm。希望不同的签名足以区分我需要的行动。

图片控制器的操作方法如下 ActionResult索引(字符串用户名){...} ActionResult索引(长id){...}

routes.MapRoute(
    "UsersPicturesRoute",
    "Pictures/{username}",
    new { controller = "Pictures", action = "Index", username = UrlParameter.Optional }
);

routes.MapRoute(
    "SinglePictureRoute",
    "Pictures/{id}",
    new { controller = "Pictures", action = "Index", id = UrlParameter.Optional }
);

有没有办法获得这个理想的结果?

1 个答案:

答案 0 :(得分:0)

您可以按以下顺序更改RegisterRoutes,然后您将获得所需的输出

routes.MapRoute(
          "SinglePictureRoute",
          "Pictures/{id}",
          new { controller = "Home", action = "abcd", id = UrlParameter.Optional },
          new { id = @"\d+" } // Parameter constraints
      );

routes.MapRoute(
         "UsersPicturesRoute",
         "Pictures/{username}",
         new { controller = "Home", action = "abcTest", username = UrlParameter.Optional }
     );