예제 #1
0
        /// <summary>
        /// 获取指定URL的功能信息
        /// </summary>
        public static IFunction GetFunction(this ControllerBase controller, string url)
        {
            url = url.StartsWith("https://") || url.StartsWith("http://")
                ? new Uri(url).AbsolutePath : !url.StartsWith("/") ? $"/{url}" : url;
            IServiceProvider    provider    = controller.HttpContext.RequestServices;
            IHttpContextFactory factory     = provider.GetService <IHttpContextFactory>();
            HttpContext         httpContext = factory.Create(controller.HttpContext.Features);

            httpContext.Request.Path   = url;
            httpContext.Request.Method = "POST";
            RouteContext     routeContext = new RouteContext(httpContext);
            IRouteCollection router       = controller.RouteData.Routers.OfType <IRouteCollection>().FirstOrDefault();

            if (router == null)
            {
                return(null);
            }
            router.RouteAsync(routeContext).Wait();
            if (routeContext.Handler == null)
            {
                return(null);
            }
            RouteValueDictionary dict       = routeContext.RouteData.Values;
            string           areaName       = dict.GetOrDefault("area")?.ToString();
            string           controllerName = dict.GetOrDefault("controller")?.ToString();
            string           actionName     = dict.GetOrDefault("action")?.ToString();
            IFunctionHandler handler        = provider.GetService <IFunctionHandler>();

            return(handler?.GetFunction(areaName, controllerName, actionName));
        }
예제 #2
0
        public async Task <bool> Check(string path, string method)
        {
            IRouteCollection router = RouteData.Routers.OfType <IRouteCollection>().First();

            HttpContext context = _httpContextFactory.Create(HttpContext.Features);

            context.Request.Path   = path;
            context.Request.Method = method;

            var routeContext = new RouteContext(context);
            await router.RouteAsync(routeContext);

            bool exists = routeContext.Handler != null;

            return(exists);
        }