Пример #1
0
 public static IEnumerable <IUrlConvention> ThatApplyTo(
     this IEnumerable <IUrlConvention> urlConventions,
     UrlContext urlContext, Configuration configuration,
     HttpConfiguration httpConfiguration)
 {
     return(configuration.UrlConventions.ThatAppliesTo(
                urlConventions, urlContext, urlContext));
 }
Пример #2
0
        public virtual string[] GetUrls(UrlContext context)
        {
            var urls = context.ActionMethod.Method
                       .GetAttribute <UrlAttribute>()?.Urls
                       .Where(x => x.IsNotNullOrWhiteSpace())
                       .Select(x => x.Trim('/')).ToArray();

            return(urls ?? new[] { context.UrlSegments.Join("/") });
        }
Пример #3
0
        public virtual string[] GetUrls(UrlContext context)
        {
            var urls = context.ActionMethod
                       .GetAttribute <UrlAttribute>()?.Urls
                       .Where(x => x.IsNotNullOrWhiteSpace())
                       .Select(x => x.Trim('/')).ToArray() ??
                       MapNamespaceToUrls(context);

            return(urls.Concat(GetUrlAliases(context)).ToArray());
        }
Пример #4
0
        protected virtual string[] MapNamespaceToUrls(UrlContext context)
        {
            var prefix     = _configuration.UrlPrefix?.Trim('/');
            var @namespace = context.ActionMethod
                             .HandlerTypeDescriptor.Type.Namespace ?? "";

            return(_configuration.NamespaceUrlMappings
                   .Where(x => x.Applies(@namespace))
                   .Select(x => x.Map(@namespace, s => new UrlSegment(s))
                           .Concat(context.MethodSegments
                                   .Select(s => s))
                           .ToUrl())
                   .Select(x => prefix.IsNotNullOrEmpty() ? prefix.JoinUrls(x) : x)
                   .ToArray());
        }
Пример #5
0
        public virtual string[] GetUrlAliases(UrlContext context)
        {
            var aliases = new List <string>();

            var attributeAliases = context.ActionMethod.GetAttribute <UrlAliasAttribute>();

            if (attributeAliases != null)
            {
                aliases.AddRange(attributeAliases.Urls);
            }

            aliases.AddRange(_configuration.UrlAliases.Select(x => x(context)));

            return(aliases.Where(x => x.IsNotNullOrWhiteSpace())
                   .Select(x => x.Trim('/')).ToArray());
        }
        public virtual List <RouteDescriptor> GetRouteDescriptors(RouteContext context)
        {
            var action           = context.ActionMethod;
            var httpMethod       = GetHttpMethod(action);
            var requestParameter = GetRequestParameter(action, httpMethod);
            var actionParameters = GetActionParameters(action, requestParameter).ToList();
            var methodSegments   = GetMethodSegments(action, actionParameters).ToList();
            var urlParameters    = GetUrlParameters(methodSegments).ToList();

            ValidateUrlParameters(action, urlParameters);

            var parameters   = GetParameters(actionParameters, urlParameters).ToList();
            var responseBody = GetResponseBody(action);

            var urlContext = new UrlContext(action, httpMethod, methodSegments,
                                            urlParameters, parameters, requestParameter, responseBody);

            return(_urlConventions.ThatApplyTo(urlContext, _configuration, _httpConfiguration)
                   .SelectMany(uc => uc.GetUrls(urlContext))
                   .Select(u => new RouteDescriptor(httpMethod, u, urlParameters,
                                                    parameters, requestParameter, responseBody)).ToList());
        }
        public virtual List <RouteDescriptor> GetRouteDescriptors(RouteContext context)
        {
            var action     = context.ActionMethod;
            var httpMethod = _configuration.GetHttpMethod(_configuration, action)
                             .AssertNotEmptyOrWhitespace("Http method not found on " +
                                                         $"action {action.HandlerType.Type.FullName}.{action.Method.Name}.")
                             .Trim().ToUpper();

            var requestParameter = !_configuration.SupportedHttpMethods
                                   .Any(x => x.AllowRequestBody && x.Method == httpMethod) ? null :
                                   action.Method.Parameters.Take(1)
                                   .Select(x => new
            {
                Type     = x,
                FromUri  = x.HasAttribute <FromUriAttribute>(),
                FromBody = x.HasAttribute <FromBodyAttribute>(),
                x.ParameterType.IsSimpleTypeOrHasSimpleElementType,
                x.ParameterType.IsComplexTypeOrHasComplexElementType
            })
                                   .FirstOrDefault(x => (x.IsSimpleTypeOrHasSimpleElementType && x.FromBody) ||
                                                   (x.IsComplexTypeOrHasComplexElementType && !x.FromUri))?.Type;

            var parameters = action.Method.Parameters.Where(x => x != requestParameter).ToList();

            var methodSegments = _configuration.GetActionMethodName(_configuration, action).
                                 Split('_').Where(x => x.IsNotNullOrWhiteSpace())
                                 .Select(x =>
            {
                var urlParameter = parameters.FirstOrDefault(p =>
                                                             p.Name.EqualsIgnoreCase(x));
                var isWildcard = urlParameter?.HasAnyAttribute <WildcardAttribute,
                                                                ParamArrayAttribute>() ?? false;
                return(new
                {
                    Segment = urlParameter == null
                            ? x : $"{{{(isWildcard ? "*" : "")}{urlParameter.Name}}}",
                    UrlParameter = urlParameter,
                    IsWildcard = isWildcard
                });
            }).ToList();

            if (methodSegments.Count(x => x.IsWildcard) > 1)
            {
                throw new InvalidOperationException("Multiple wildcard parameters found on " +
                                                    $"action {action.HandlerType.FriendlyFullName}.{action.Method.FriendlyName}.");
            }

            var querystringParameters = parameters.Where(p =>
                                                         !methodSegments.Any(s => s.UrlParameter == p)).ToArray();

            var urlParameters = parameters.Where(p => methodSegments
                                                 .Any(s => s.UrlParameter == p)).ToArray();

            var wildcardParameters = parameters.Where(p => methodSegments
                                                      .Any(s => s.IsWildcard)).ToArray();

            var urlSegments = _configuration.GetHandlerNamespace(_configuration, action)
                              .Split('.').Where(x => x.IsNotNullOrWhiteSpace())
                              .Concat(methodSegments.Select(x => x.Segment)).ToArray();

            var responseBody = !action.Method.HasResult ? null : action.Method.ReturnType;

            var urlContext = new UrlContext(_configuration, context.HttpConfiguration,
                                            action, httpMethod, urlSegments, urlParameters, wildcardParameters,
                                            querystringParameters, requestParameter, responseBody);

            return(_urlConventions.ThatApplyTo(urlContext, _configuration)
                   .SelectMany(uc => uc.GetUrls(urlContext))
                   .Select(u => new RouteDescriptor(httpMethod, u, urlParameters,
                                                    wildcardParameters, querystringParameters, requestParameter,
                                                    responseBody)).ToList());
        }
Пример #8
0
 public virtual string[] GetUrls(UrlContext context)
 {
     return(_getUrls(context.ActionMethod, context.UrlSegments));
 }
Пример #9
0
 public virtual bool AppliesTo(UrlContext context)
 {
     return(_appliesTo?.Invoke(context.ActionMethod) ?? true);
 }
Пример #10
0
 public virtual bool AppliesTo(UrlContext context)
 {
     return(true);
 }