简体   繁体   English

asp.net路由问题和冲突

[英]asp.net routing issue and conflict

i am using asp.net routing to show some pages, but it is conflicting , the first 2 are working but the third one goes to the 2nd route eventhough i have a seperate page for it to work. 我使用asp.net路由来显示一些页面,但它是冲突的,前两个正在工作,但第三个进入第二个路径,尽管我有一个单独的页面,它可以工作。

routes.MapPageRoute(
          "post",
          "{postname}-{postid}/",
          "~/post.aspx"
      );

routes.MapPageRoute(
          "Posts",
          "{Category}/{PageNo}/",
          "~/posts.aspx", true,
            new RouteValueDictionary { { "PageNo", "" } }
      );

routes.MapPageRoute(
          "News",
          "{News-Category}/{PageNo}/",
          "~/news.aspx", true,
            new RouteValueDictionary { { "PageNo", "" } }
      );

any help is appreciated 任何帮助表示赞赏

thanks 谢谢

The Posts and News routes have exactly the same URL signature. PostsNews路由具有完全相同的URL签名。 That is, any URL with 2 segments will always match the Posts route and the News route is an unreachable execution path. 也就是说,具有2个段的任何 URL将始终与Posts路由匹配,并且News路由是不可到达的执行路径。

You need to either use 1 or more constant segments or 1 or more constraints to ensure there are 2-segment routes Posts and News routes can miss. 您需要使用一个或多个常量段或一个或多个约束来确保有2段路径的PostsNews路线可能会错过。

routes.MapPageRoute(
      "post",
      "{postname}-{postid}",
      "~/post.aspx"
  );

routes.MapPageRoute(
      "Posts",
      "Category/{PageNo}",
      "~/posts.aspx", true,
        new RouteValueDictionary { { "PageNo", UrlParameter.Optional } }
  );

routes.MapPageRoute(
      "News",
      "News-Category/{PageNo}",
      "~/news.aspx", true,
        new RouteValueDictionary { { "PageNo", UrlParameter.Optional } }
  );

Think of the routes like a switch case statement. 想想路由就像switch case语句一样。 If the condition matches, the route will return. 如果条件匹配,则路线将返回。 But if you have 2 routes that will both match the same condition, the first one will always win and the second one will be unreachable. 但是如果你有2条路线都匹配相同的条件,第一条路线将永远胜利而第二条路线将无法到达。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM