2012-02-21 99 views
1

我有类似下面的地址:在MVC路由中设置区域注册时可以使用通配符吗?

www.stack.com/content-00000/solutions-about 

我的路线注册(未经证实,但也许90%还行)看起来是这样的:

public class ContentAreaRegistration : AreaRegistration 
    { 
     public override string AreaName 
     { 
      get 
      { 
       return "Content"; 
      } 
     } 

     public override void RegisterArea(AreaRegistrationContext context) 
     { 
      context.MapRoute(
       "Content_default", 
       "content-{id}/{title}", 
       new { controller = "Server", action = "Get" }, 
       new { id = @"^\d{5}$" } 
       new { title = UrlParameter.Optional } 
      ); 
     } 
    } 

有人能确认这是否是正确的方法对我来说做路线

a) Go to the "Content" Area 
b) Go to the "Server" Controller 
b) Go to the "Get " action 
c) Five digits that follow will be put into a parameter called id? 

此外,我怎么能做出这种去到另一个动作,如“错误”如果没有后面五个数字的“内容 - ”?

回答

2

你从a)指向d)看起来正确。 要实现最后,我想补充一个更规则规则立即后你出,这说明完全一致,但没有正则表达式,并可能与标记“身份证”作为可选的,是这样的: `

context.MapRoute(
       "Content_Error",//or maybe *null* here 
       "content-{id}/{title}", 
       new { controller = "Server", action = "Get" }, 
       new { id = UrlParameter.Optional, 
        title = UrlParameter.Optional } 
      ); 

`

由于这种规则下你的主要规则,它只会如果ID未满足第一条规则您正则表达式执行。