views:

99

answers:

6

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!

A: 

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

eglasius
I'm not using MVC
Andrey
A: 

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}$"}}
       );
}
ntziolis
I already tried it as I've mentioned at the top - it doesn't work
Andrey
Can you try and remove the regex constraint to see if the routing works in general? If not that its not the regex thats causing the issue, but the fact that the routing cant handle "empty query strings" that way.
ntziolis
Overall routing works, I use it for this URL without constraints right now and default to "personal" if it's not one of the above. But I wanted to restrict url to no tab or only from that list, and that I can't figure out
Andrey
A: 

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.

Martin Peck
I think you presume I am using MVC, which I'm not.
Andrey
You're right - I did. Sorry. You should - it's great!
Martin Peck
A: 

Hey,

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

sTodorov