开发者

Asp.net Routing - Route constraints and empty Route

开发者 https://www.devze.com 2023-01-17 06:24 出处:网络
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 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开发者_StackOverflow社区 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!


[ ] is for character set.

use ( ) instead

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


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...

  • http://localhost/member/Edit?tabvalue=Professional
  • http://localhost/member/Edit?tabvalue=Personal
  • http://localhost/member/Edit?tabvalue=Values

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

  • http://localhost:51590/Home/Edit?tabvalue=Cheese
  • http://localhost:51590/Home/Edit

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


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


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}$"}}
       );
}


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消