0

我有以下路由配置:

routes.MapRoute(
               name: "Pages",
               url: "{PageName}",
               defaults: new { controller = "Pages", action = "GetPageByName" }
            );

我需要更改网站结构。我需要将下面的 URL 路由到“ pagesController”。两种情况下要发送到控制器的参数都是“PageName”:

http://domain.com/PageName/
http://domain.com/something/PageName

“某事”不是必需的,但可以添加但没有效果。

这可以做到吗?

4

2 回答 2

0

尝试这个:

                routes.MapRoute(
                   name: "Pages",
                   url: "{something}/{PageName}",
                   defaults: new { controller = "Pages", action = "GetPageByName" }
                );

                routes.MapRoute(
                   name: "Pages",
                   url: "{PageName}",
                   defaults: new { controller = "Pages", action = "GetPageByName" }
                );

您必须按该顺序定义,该路线将映射到GetPageByName您的操作方法PagesController

public ActionResult GetPageByName(string pageName)
{

}
于 2013-11-04T21:22:12.440 回答
0

只是设法做到了:

 routes.MapRoute(
               name: "Pages",
               url: "{stringURL}",
               defaults: new { controller = "Pages", action = "GetPageByStringURL" }
            );

            routes.MapRoute(
               name: "Pages2",
               url: "{x}/{stringURL}",
               defaults: new { controller = "Pages", action = "GetPageByStringURL" }
            );
于 2013-11-04T21:28:22.037 回答