为什么我的路由器没有重定向到我的页面?

时间:2013-10-12 15:29:07

标签: c# asp.net webforms routes

我有一个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。

但是,我点击其他页面并重定向到我的首页。我无法找到我正在点击的页面,这有点烦人。

从这些稀疏信息中,我的路线中是否存在看似错误的内容?当我输入我写的网址时,最后一个不应该工作吗?

我认为没有任何兴趣,但这是我的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";


        }

2 个答案:

答案 0 :(得分:4)

路线的顺序很重要。 您将始终被重定向到LaesBlog Route。 所以你需要把它放在最后这样

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

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

希望有所帮助

答案 1 :(得分:1)

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

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

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

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

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