ASP.NET 4.0 URL Routing - Similar MapPageRoutes

半世苍凉 提交于 2019-11-29 17:00:22

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!