简体   繁体   中英

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.

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. That is, any URL with 2 segments will always match the Posts route and the News route is an unreachable execution path.

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.

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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