asp.net webforms routing: optional parameters

前端 未结 3 490
深忆病人
深忆病人 2020-12-10 13:21

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/el

3条回答
  •  旧巷少年郎
    2020-12-10 14:13

    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

提交回复
热议问题