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
653 views
in Technique[技术] by (71.8m points)

asp.net webforms routing: optional parameters

I want to add optional parameters in my routing table. For example I would like the users to browse a product catalog like this: http://www.domain.com/browse/by-category/electronics/1,2,3 etc

Now i've created a route like this:

            routes.MapPageRoute(
           "ProductsBrowse",
            "browse/{BrowseBy}/{Category}",
            "~/Pages/Products/Browse.aspx"
        );

Problem however is that when a user enters http://www.domain.com/browse , I would like them to present a different page where they can pick the manner on how to browse. So the parameters {BrowseBy} and {Category} will not be used.

Is there a way around this then to create seperate routes for each of the scenarios?

Thank you for your time! Kind regards, Mark

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

I just came across this question, and knew there had to be way to do this. There is-

MapPageRoute has an overload that will allow you to specify defaults. here's an example usage based on your code:

routes.MapPageRoute(
       "ProductsBrowse",
        "browse/{BrowseBy}/{Category}",
        "~/Pages/Products/Browse.aspx",
        false,
        new RouteValueDictionary { { "Category", string.Empty } }
    );

So if the user doesn't specify a category this route will still be hit. The problem I have with using two separate routes is that I have links setup around my site that are generated by route name, and you cannot have two routes that have the same name.

Here's good documentation from MSDN: here


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

1.4m articles

1.4m replys

5 comments

56.9k users

...