Routing based on same parameter(s) pattern and different pages in web forms

一曲冷凌霜 提交于 2019-12-11 09:16:03

问题


I am implementing Routing in web forms :

These is my custom routings

public static void MyCustomRoutes(RouteCollection routes)
        {
            routes.Ignore("{resource}.axd/{*pathInfo}");
            routes.MapPageRoute("NewsByTitle",
                            "{NewsTitle}",
                             "~/News.aspx");

            routes.MapPageRoute("BlogsByTitle",
                               "{BlogsTitle}",
                                "~/ViewBlogs.aspx"); 
        }

In my default page i'm having Blogs and News sections , when i'm clicking on News it is navigating to the News page as it is defined first in the routing table.

but when i'm clicking on Blogs it is taking the route of News only .

Here is my RedirectToRoute for Blogs and News

News:

  String Url = clsMethods.GetTileByStoryId(BlogId);  //My Url Param
Response.RedirectToRoute("NewsByTitle", new { NewsTitle = Url });

Blogs

   String Url = clsMethods.GetTileByStoryId(BlogId);
  Response.RedirectToRoute("BlogsByTitle",  new { BlogsTitle = Url});

Update

As per Mihir Suggestion i have created the Custom Constraint so that will be solving my need here this is how i implemented the Constraint Logic

 public static void MyCustomRoutes(RouteCollection routes)
            {
                routes.Ignore("{resource}.axd/{*pathInfo}");
                    routes.MapPageRoute("NewsByTitle",
                            "{NewsTitle}",
                             "~/News.aspx",
                             false,
                             null,
                             new RouteValueDictionary
              { { "checkNewsRoute", new   IsNewsConstraint() } });

             routes.MapPageRoute("BlogsByTitle",
                               "{BlogsTitle}",
                                "~/ViewBlogs.aspx", 
                                false,
                               null,
                               new RouteValueDictionary
                      { { "checkRoute", new IsBlogConstraint()} });

            }

Here is that Constraint

Blogs Constraint

 public class IsBlogConstraint : IRouteConstraint
    {
        public bool Match
            (
                HttpContextBase httpContext,
                Route route,
                string parameterName,
                RouteValueDictionary values,
                RouteDirection routeDirection
            )
        {
            return clsMethods.checkRoute(Convert.ToString(values["BlogsTitle"])); 

        }
    }

News Constraint

  public class IsNewsConstraint : IRouteConstraint
    {
        public bool Match
            (
                HttpContextBase httpContext,
                Route route,
                string parameterName,
                RouteValueDictionary values,
                RouteDirection routeDirection
            )
        {
            return clsMethods.checkNewsRoute(Convert.ToString(values["NewsTitle"]));
        }
    }

回答1:


I think you are trying to configure your route to map following URL pattern.

  1. News: http: //www.website.com/title-of-the-news
  2. Blog: http: //www.website.com/title-of-the-blog

As per your configuration if you try to open the blog it will take you to the news page which is a correct behaviour because URL to the blog does match with news route.

To distinguish two different pages you need to configure route with some specific path for both types news and blog like I have done below.

routes.MapPageRoute("NewsByTitle", "news/{NewsTitle}", "~/News.aspx");
routes.MapPageRoute("BlogsByTitle", "blogs/{BlogsTitle}", "~/ViewBlogs.aspx");
  1. News: http: //www.website.com/news/title-of-the-news
  2. Blog: http: //www.website.com/blogs/title-of-the-blog

If you are looking for solution exactly you have mentioned in your question you need to implement route constraints as it is done here. Custom Constraints help routes to prevent a route from being matched unless some custom condition is matched, blog or news in your case.

In that constraint you can write some logic to check that the path segment is a news or a blog and return boolean value. So when the news constraint looks for a blog name it would not find the entry as news (in the db) and returns false and that route would be ignored.



来源:https://stackoverflow.com/questions/40317924/routing-based-on-same-parameters-pattern-and-different-pages-in-web-forms

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!