public UrlLookup( IOptions <UrlOptions> urlOptions, IUrlMaps urlMaps) { _urlOptions = urlOptions.Value; _urlMaps = urlMaps; }
public UrlBuilder( TInput request, UrlOptions urlOptions, IUrlMaps urlMaps) { Request = request; _urlOptions = urlOptions; _urlMaps = urlMaps; }
public static IServiceCollection AddMiruUrls( this IServiceCollection services, Action <UrlOptions> options = null) { var routesInfo = new Dictionary <string, ModelToUrlMap>(); var convention = new MiruRoutingDiscoverer(routesInfo); services.AddSingleton <IActionContextAccessor, ActionContextAccessor>(); services.AddScoped(x => { var actionContext = x.GetRequiredService <IActionContextAccessor>().ActionContext; var factory = x.GetRequiredService <IUrlHelperFactory>(); return(factory.GetUrlHelper(actionContext)); }); services.AddSingleton <UrlLookup>(); services.AddSingleton <IUrlMaps, DefaultUrlMaps>(); services.AddSingleton(convention); services.Configure <MvcOptions>(x => x.Conventions.Add(convention)); var urlOptions = new UrlOptions(); options?.Invoke(urlOptions); services.AddSingleton(urlOptions); services.AddSingleton(new QueryStringConfig()); return(services); }
public RouteValueDictionary Generate( object model, string prefix = "", RouteValueDictionary dict = null, UrlOptions urlOptions = null, List <Accessor> withoutProperties = null, Dictionary <Accessor, object> withProperties = null, List <KeyValuePair <Accessor, object> > withoutPropertyAndValues = null) { if (model == null) { return(dict); } Type modelType = model.GetType(); dict ??= new RouteValueDictionary(); var filters = urlOptions?.QueryStrings.GetQueryStringBuilderFilters(model); var filtersWhenModified = urlOptions?.QueryStrings.GetIgnoredWhenModified(); var hasWithOrWithout = (withProperties != null && withProperties.Any()) || (withoutProperties != null && withoutProperties.Any()) || (withoutPropertyAndValues != null && withoutPropertyAndValues.Any()); foreach (PropertyInfo p in PropertyCaches.GetOrAdd(modelType, t1 => System.Reflection.TypeExtensions.GetProperties(t1))) { var theCache = Cache.GetOrAdd(modelType.FullName + prefix + p.Name, _ => new CacheOfTypes { Attributes = p.GetCustomAttributes().ToArray(), PropType = IsEnum(p.PropertyType) || IsConvertible(p.PropertyType) ? PropType.Simple : (IsEnumerable(p.PropertyType) ? PropType.Enumerable : (SimpleGetter(p) ? PropType.Complex : PropType.Unknown)), MemberAccessor = new MemberAccessor(modelType, p.Name) }); var propType = theCache.PropType; var attributes = theCache.Attributes; var accessor = theCache.MemberAccessor; var originalValue = accessor.Get(model); if (ShouldExcludeProperty(filters, p, originalValue)) { continue; } if (propType == PropType.Simple) { var val = GetValueForSimpleProperty(p, accessor, withProperties, model); if (ShouldIgnoreProperty(p, filters, filtersWhenModified, hasWithOrWithout, withoutProperties, withoutPropertyAndValues, val)) { continue; } if (val == null || Equals(val, GetDefault(p.PropertyType)) && attributes.OfType <FromRouteAttribute>().Any() == false) { continue; } dict.Add(prefix + p.Name, val); } else if (propType == PropType.Enumerable) { var i = 0; var added = new List <object>(); foreach (object sub in (IEnumerable)accessor.Get(model) ?? new object[0]) { if (sub == null) { continue; } var subType = sub.GetType(); var subPropType = IsEnum(subType) || IsConvertible(subType) ? PropType.Simple : PropType.Unknown; if (subPropType == PropType.Simple) { if (ShouldIgnoreProperty(p, filters, filtersWhenModified, hasWithOrWithout, withoutProperties, withoutPropertyAndValues, sub)) { continue; } added.Add(sub); dict.Add(prefix + p.Name + "[" + (i++) + "]", sub); } else { Generate(sub, prefix + p.Name + "[" + (i++) + "].", dict); } } if (withProperties != null) { var withCustomProperties = withProperties.Where(a => a.Key.Name == p.Name); foreach (var withCustomProperty in withCustomProperties) { if (added.Contains(withCustomProperty.Value)) { continue; } dict.Add(prefix + p.Name + "[" + (i++) + "]", withCustomProperty.Value); } } } else if (propType == PropType.Complex) { Generate(accessor.Get(model), prefix + p.Name + ".", dict); } } // add with if (withProperties != null) { foreach (var withProperty in withProperties?.Where(p => p.Key.PropertyType.Implements <IEnumerable>() == false)) { // if key/value was not added yet if (!dict.Any(x => x.Key == withProperty.Key.Name && x.Value.Equals(withProperty.Value))) { dict.Add(withProperty.Key.Name, withProperty.Value.ToString()); } } } return(dict); }