基于相同参数模式和Web表单中不同页面的路由 [英] Routing based on same parameter(s) pattern and different pages in web forms

查看:103
本文介绍了基于相同参数模式和Web表单中不同页面的路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在以Web形式实现路由:

I am implementing Routing in web forms :

这些是我的自定义路由

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

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

在我的默认页面中,当我m点击新闻,它会导航到新闻页面,因为它是在路由表中首先定义的。

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 .

这是我的 RedirectToRoute 用于博客新闻

新闻:

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

博客

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

更新

根据Mihir建议,我创建了自定义约束,因此将在这里解决我的需求,这就是我实施约束逻辑的方式

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()} });

            }

这里是约束

博客约束

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

        }
    }

新闻约束

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


推荐答案

I认为您正在尝试按照以下URL模式配置要映射的路线。

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


  1. 新闻: http://www.website.com/title-of-the-news

  2. 博客: http:// www。 website.com/title-of-the-blog

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

根据您的配置,如果您尝试打开博客会将您带到新闻页面,这是正确的行为,因为博客的URL确实与新闻路线匹配。

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. 新闻:http://www.website.com/news/新闻标题

  2. 博客:http://www.website.com/blogs/新闻标题

如果您正在寻找问题中确实提到的解决方案,则需要在完成后实施路由约束此处自定义约束可以帮助路由防止匹配某条路由,除非您匹配了某些自定义条件,博客或新闻。

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.

在该约束中,您可以编写一些逻辑来检查路径段是新闻还是博客,并返回布尔值。因此,当新闻约束条件查找博客名称时,它将不会在新闻数据库中找到该条目作为新闻,并返回false,并且该路由将被忽略。

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.

这篇关于基于相同参数模式和Web表单中不同页面的路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆