概念/述语
从这里开始
数据库
开发入门
程序扩展
使用介绍
其它

基础路由

路由在ZKEACMS纸壳CMS中扮演着重要的角色,它将用户请求的地址路由到对应的控制器(Controller)中进行处理。

先看一下纸壳CMS中的基础路由定义RouteDescriptors代码

public static class RouteDescriptors
{
    public static List<RouteDescriptor> Routes { get; } = new List<RouteDescriptor>
    {
            new LocalizeRouteDescriptor
            {
            RouteName = "pageRoute",
            Template = "{*path}",
            Defaults = new { controller = "page", action = "main" },
            Constraints = new { path = new PageRouteConstraint() },
            Priority = -2
            },
            new RouteDescriptor
            {
            RouteName = "admin",
            Template = "admin/{controller=dashboard}/{action=index}/{id?}",
            Defaults=new { module = "admin" },
            Priority = 10
            },
            new RouteDescriptor
            {
            RouteName = "Validation",
            Template = "validation/{action}",
            Defaults = new { controller = "validation" },
            Priority = 11
            },
            new RouteDescriptor
            {
            RouteName = "error",
            Template = "error/{action}/{code?}",
            Defaults = new { controller = "error", action = "index" },
            Priority = 11
            },
            new LocalizeRouteDescriptor
            {
            RouteName = "AccountAdmin",
            Template = "account/{action}",
            Defaults = new { controller = "account", action = "index"},
            Priority = 11
            }
    };
}

路由将按优先(Priority)级从高到低进行匹配,最低的是pageRoute页面路由,它用于处理用户创建的页面。它是一个全捕捉路由,所有没有匹配到路由的请求都会由pageRoute进行处理。

插件中的路由

每一个插件都可以定义自己的路由。路由的定义写在插件类(*Plug:PluginBase)中,例如Shop插件中定义的相关路由:

namespace ZKEACMS.Shop
{
    public class ShopPlug : PluginBase
    {
        public override IEnumerable<RouteDescriptor> RegistRoute()
        {
            yield return new RouteDescriptor
            {
                RouteName = "Basket",
                Template = "basket/{action}",
                Defaults = new { controller = "basket", action = "add" },
                Priority = 11
            };
            yield return new RouteDescriptor
            {
                RouteName = "AliPay",
                Template = "alipay/{action}/{orderId?}",
                Defaults = new { controller = "alipay", action = "pay" },
                Priority = 11
            };
            yield return new RouteDescriptor
            {
                RouteName = "CustomOrder",
                Template = "myorder/{action}/{Id?}",
                Defaults = new { controller = "customorder", action = "index" },
                Priority = 11
            };
        }
    }
}

微信公众号