Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
865 views
in Technique[技术] by (71.8m points)

c# 4.0 - ASP.NET 4.0 URL Routing - Similar MapPageRoutes

I will try to explain this the best I can.

I created a CMS that allows you to create Categories and Content Sections. Both have completely different templates, but I want to use the same URL routing mapPageRoute param when routing. Basically, I need it to check if the alias is a category, if not hit the content section router.

Here is my Registered Routes on Global.asax:

void RegisterRoutes(RouteCollection routes)
{
    routes.MapPageRoute(
        "Home",
        string.Empty,
        "~/Default.aspx"
    );

    routes.MapPageRoute(
        "Category",
        "{*CategoryAlias}",
        "~/templates/Category.aspx"
    );

    routes.MapPageRoute(
        "Content",
        "{*ContentAlias}",
        "~/templates/Content.aspx"
    );
}

Currently, Categories work fine, but when I put a content section alias in the URL it hits categories and doesn't skip to the next route to try. The Category.aspx and Content.aspx web forms have completely different views. The code behind is similar but one accesses the Category tables/procedures and the other Content.

If anyone requires more information just ask.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Have you tried something like this?

void RegisterRoutes(RouteCollection routes) 
{ 
    routes.MapPageRoute( 
        "Home", 
        string.Empty, 
        "~/Default.aspx" 
    ); 

    routes.MapPageRoute( 
        "Category", 
        "Category/{Cat}/{*queryvalues}", 
        "~/templates/Category.aspx" 
    ); 

    routes.MapPageRoute( 
        "Content", 
        "Content/{Cont}{*queryvalues}", 
        "~/templates/Content.aspx" 
    ); 
} 

And then make sure the URLs have either Category or Content in the path. You still get the catch-all with *queryvalues

EDIT:

If you have the following uri http://www.example.com/Content/Press you can access Press by using the following:

Page.RouteData.Values["Cont"].ToString();

So, in your Content.aspx page, grab that string and then use that to determine which site the user was trying to get to.

You need to include some kind of static URL differentiator so that the MapRouter can find where to map the page.

If you don't include the static Category or Content in the beginning of the uri, the MapRouter will always be satisfied with the first map (the Category mapping) and never know to skip it.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...