【问题标题】:Url Routing Rule is conflicting网址路由规则冲突
【发布时间】:2011-11-09 09:29:19
【问题描述】:

我正在我的 asp.net 应用程序中实现 URL 路由,因为我的一些规则是冲突的。 以下是我的一些冲突规则:

RouteTable.Routes.Add("QuestionSubject",
    new Route("questions/{subjectname}/{pageno}",
    new RouteValueDictionary { { "pageno", null } },
    new RouteValueDictionary { { "pageno", @"^[0-9]*$" } },
    new EventRouteHandler("~/questionsitemap/subject.aspx")));

RouteTable.Routes.Add("QuestionSubjectTopic",
    new Route("questions/{subjectname}/{topicname}/{pageno}",
    new RouteValueDictionary { { "pageno", null } },
    new RouteValueDictionary { { "pageno", @"^[0-9]*$" } },
    new EventRouteHandler("~/questionsitemap/topic.aspx")));

RouteTable.Routes.Add("QuestionGrade",
    new Route("questions/{gradename}/",
    new EventRouteHandler("~/questionsitemap/grade.aspx")));

RouteTable.Routes.Add("QuestionSubjectGrade",
    new Route("questions/{gradename}/{subjectname}/",
    new EventRouteHandler("~/questionsitemap/grade.aspx")));

RouteTable.Routes.Add("QuestionSubjectTopicGrade",
    new Route("questions/{gradename}/{subjectname}/{topicname}/",
    new EventRouteHandler("~/questionsitemap/grade.aspx")));

当我请求打开 QuestionGrade 规则时,它会调用 QuestionSubject,因为两者的规则相似,其他规则也是如此。

请任何人帮助我找出解决方案。

【问题讨论】:

    标签: c# asp.net url url-routing


    【解决方案1】:

    您需要使它们更具体,因为路由引擎无法区分{subjectname}{gradename}。您可以使用类似以下的内容,将/subject//grade/ 添加到您的路线中吗?

    RouteTable.Routes.Add("QuestionSubject",
        new Route("questions/subject/{subjectname}/{pageno}",
        new RouteValueDictionary { { "pageno", null } },
        new RouteValueDictionary { { "pageno", @"^[0-9]*$" } },
        new EventRouteHandler("~/questionsitemap/subject.aspx")));
    
    RouteTable.Routes.Add("QuestionGrade",
        new Route("questions/grade/{gradename}/",
        new EventRouteHandler("~/questionsitemap/grade.aspx")));
    

    【讨论】:

    • :但我的客户不想要任何静态目录,如“主题”或“等级”
    • 如果您使用这样的路由,您将别无选择。第一个“questions/{}/{}”结构总是会获胜。必须有某种其他程度的消歧。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-04
    • 2017-04-15
    • 2018-03-25
    • 2018-06-07
    相关资源
    最近更新 更多