2

我有一个 ASP.NET Web 表单项目,具有以下路线。这通常工作正常,但我不能让最后一个工作:

static void RegisterRoutes(RouteCollection routes)
{
    RouteTable.Routes.Ignore("{resource}.axd/{*pathInfo}");

    routes.MapPageRoute("Sidevisning",
                        "{" + Utils.Constants.RouteVariables.Article + "}",
                        "~/ArticleLayout.aspx");



    routes.MapPageRoute("Tags",
                        "tags/{" + Utils.Constants.RouteVariables.Tags + "}",
                        "~/TagsPage.aspx");

    routes.MapPageRoute("Forumpost",
                        "forums/topic/{" + Utils.Constants.RouteVariables.Post + "}",
                        "~/ForumPost.aspx");

    routes.MapPageRoute("Bruger",
                        "users/{" +
                        Utils.Constants.RouteVariables.UserProfileId + "}",
                        "~/UserProfile.aspx");

    routes.MapPageRoute("Offer",
                        "offers/{" + Utils.Constants.RouteVariables.OfferForm + "}",
                        "~/Offer.aspx");


    routes.MapPageRoute("Ret opgaver",
                        "assignments/{" + Utils.Constants.RouteVariables.Assignment + "}",
                        "~/ReviewAssignments.aspx");


    routes.MapPageRoute("Tutoring with subject",
                        "tutoring/{" + Utils.Constants.RouteVariables.TutorSubject + "}",
                        "~/tutoring/tutors.aspx");

    routes.MapPageRoute("Tutoring with subject and city",
                        "tutoring/{" + Utils.Constants.RouteVariables.TutorSubject + "}/{" + Utils.Constants.RouteVariables.TutorCity + "}",
                        "~/tutoring/tutors.aspx");

    routes.MapPageRoute("Tutoring with subject, city and level",
                        "tutoring/{" + Utils.Constants.RouteVariables.TutorSubject + "}/{" + Utils.Constants.RouteVariables.TutorCity + "}/{" + Utils.Constants.RouteVariables.TutorLevel + "}",
                        "~/tutoring/tutors.aspx");


    routes.MapPageRoute("Blog kategori",
                "category/{BlogCategoryName}",
                "~/BlogCategory.aspx");



    // be careful to switch around , it might give some pretty fucked up errors



    routes.MapPageRoute("Laes Blog",
        "{Year}/{Month}/{BlogHeadline}",
        "~/ReadBlogEntry.aspx");


    routes.MapPageRoute("Subject details",
                  "subject/{" + Utils.Constants.RouteVariables.SubjectChosen + "}/{" + Utils.Constants.RouteVariables.SubjectAction + "}",
                  "~/SubjectDetails.aspx");


}

因此,当我输入 URL 时,例如:

http://localhost:5744/subject/danskundervisning/articleoverview

我想点击 SubjectDetails.aspx。

然而,相反,我点击了其他页面并重定向到我的首页。我也找不到我正在点击的页面,这有点烦人。

从这些稀疏的信息中,我的路线是否有问题?当我输入我写的 URL 时,最后一个不应该工作吗?

我认为它没有任何兴趣,但这是我的 RouteVaraibles:

 public class RouteVariables
        {
            public const string Article = "article";
            public const string OfferForm = "offers";
            public const string Forumcategory = "forumname";
            public const string Post = "posturl";
            public const string UserProfileId = "userid";
            public const string UserProfileProfilename = "userprofilename";
            public const string Tags = "tags";
            public const string Assignment = "assignment";
            public const string TutorSubject = "tutorsubject";
            public const string TutorCity = "tutorcity";
            public const string TutorLevel = "tutorlevel";

            public const string SubjectAction = "subjectionaction";

            public const string SubjectChosen = "subjectchosen";


        }
4

2 回答 2

4

路线的顺序很重要。您总是会被重定向到 LaesBlog 路线。所以你需要像这样把那个放在最后

routes.MapPageRoute("Subject details",
              "subject/{" + Utils.Constants.RouteVariables.SubjectChosen + "}/{" + Utils.Constants.RouteVariables.SubjectAction + "}",
              "~/SubjectDetails.aspx");

routes.MapPageRoute("Laes Blog",
    "{Year}/{Month}/{BlogHeadline}",
    "~/ReadBlogEntry.aspx");

希望能帮助到你

于 2013-10-12T16:17:44.347 回答
1

我也面临同样的问题。我更改了路由定义的顺序,它起作用了。这里的问题是路线“Laes Blog”具有所有动态参数,而路线“主题详细信息”具有一个静态(硬编码)参数和 2 个动态参数。但是两者都有3个参数,所以会发生冲突。

总而言之,最后总是写没有任何静态参数的路由。

我最近在我的博客上发布了一篇关于同一问题的文章,链接如下

http://karmic-development.blogspot.in/2013/10/url-routing-in-aspnet-web-forms-same.html

我还在之前的博客文章中发布了我在实现 URL 路由时遇到的其他问题。

于 2013-10-13T13:03:51.893 回答