src/ZKEACMS/Route/PageRouteConstraint.cs

(完整代码) 1KB

/* http://www.zkea.net/ 
 * Copyright (c) ZKEASOFT. All rights reserved. 
 * http://www.zkea.net/licenses */

using Easy.Extend;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Routing;
using Microsoft.Extensions.DependencyInjection;
using System.Collections.Generic;
using ZKEACMS.Route;
using System.Linq;
using ZKEACMS.Page;

namespace ZKEACMS
{
    public class PageRouteConstraint : IRouteConstraint
    {
        const string _homePath = "/";

        public bool Match(HttpContext httpContext, IRouter route, string routeKey, RouteValueDictionary values, RouteDirection routeDirection)
        {
            if (routeDirection == RouteDirection.UrlGeneration) return false;

            string path = $"{_homePath}{values[routeKey]}";

            if (path != _homePath)
            {
                var routeDataProviders = httpContext.RequestServices.GetService<IEnumerable<IRouteDataProvider>>();
                foreach (var item in routeDataProviders.OrderBy(m => m.Order))
                {
                    path = item.ExtractVirtualPath(path, values);

                    if (path.IsNullOrEmpty()) path = _homePath;
                }
            }

            values[routeKey] = path.ToLower();

            if (path == _homePath) return true;

            return httpContext.RequestServices.GetService<IPageService>().IsExists(path);
        }
    }
}

24 小时内免费解锁 3 个项目,之后 1 算力/个。 规则说明

AI 解读 AI

登录后可用,每次 10 算力,解读结果公开显示在下面。

还没有人解读过这个文件。