在MVC路由中设置区域注册时,我可以使用通配符吗?

时间:2012-02-21 02:29:36

标签: asp.net-mvc asp.net-mvc-3

我的地址如下:

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?

如果“内容 - ”后面没有五个数字,我该如何进行另一个操作,如“错误”?

1 个答案:

答案 0 :(得分:2)

你从a)指向d)看起来正确。 要实现最后一部分,我会在之后立即添加另一条规则,这将显示完全相同,但没有正则表达式,并且标记为“id”可选,如: `

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

`

由于此规则遵循您的主要规则,因此仅当ID在第一个规则中不满足您的正则表达式时才会执行。