0
votes

I have a url that I want to map routing to:

http://siteurl.com/member/edit.aspx?tab=tabvalue

where tabvalue is one of: "personal", "professional", "values" or nothing.

I want to map it to a route like:

Member/Edit/{tab}

But my problem is - I don't know how to specify such constraints. I'm trying this regex:

^[personal|professional|values]{0,1}$

but it only works when I use url

http://siteurl.com/member/edit/personal 

-or-

http://siteurl.com/member/edit/professional 

and doesn't work for

http://siteurl.com/member/edit/

Any ideas how to specify the correct constraint?

P.S. I'm not using MVC, just asp.net WebForms

Thanks!

6
Why {0,1} and not just a ??configurator

6 Answers

2
votes

[ ] is for character set.

use ( ) instead

^(personal|professional|values){0,1}$

1
votes

It's possible this doesn't quite meet your requirements, but if you build an enum such as this...

public enum TabValue
{
    Personal,
    Professional,
    Values,
}

... and define your Action as ...

public ActionResult Edit(TabValue? tabvalue)
{
    return View("Index");
}

... then the nullable value type of TabValue? will ensure that the following urls...

... all supply a value for tabvalue (and the casing here isn't import), where as these urls..

... hit your action with a tabvalue of null. No special routing is required to make this work.

0
votes

try specifying a default value of UrlParameter.Optional in the route declaration for tab.

ps. it should work, but maybe you have to do the above explicitely

0
votes

Try this:

public static void RegisterRoutes(RouteCollection routes)
{
    routes.MapPageRoute("",
        "Member/Edit/{tab}",
        "~/member/edit.aspx",
        true,
        new RouteValueDictionary 
            {{"tab", ""}},
        new RouteValueDictionary 
            {{"tab", "^[personal|professional|values]{0,1}$"}}
       );
}
0
votes

I have used one framework before to do this. I am not sure if you want to use a framework or if you are using one already but check this out:

http://weblogs.asp.net/scottgu/archive/2007/02/26/tip-trick-url-rewriting-with-asp-net.aspx

I have used it on a website, it is relatively easy to set up -- all rules are specified in the web.config file and you have certain freedom in designing your routes.

Hope it helps

0
votes

Consider having 3 (or 4) routes. If the value of {tab} is not dynamic at runtime, having 3 static routes is cleaner than a regex. The regex is usually only useful when there are many values at runtime, such as matching a number, date, etc.