示例#1
0
        public static async Task <string> ForAsync <T>(this IUrlHelper urlHelper, T model, bool bindExistingQueryString = false, params Action <T>[] modifiers) where T : class
        {
            if (bindExistingQueryString)
            {
                var meta         = urlHelper.ActionContext.HttpContext.RequestServices.GetRequiredService <IModelMetadataProvider>();
                var modelfactory = urlHelper.ActionContext.HttpContext.RequestServices.GetRequiredService <IModelBinderFactory>();
                var validator    = urlHelper.ActionContext.HttpContext.RequestServices.GetRequiredService <IObjectModelValidator>();

                var modelBound = await ModelBindingHelper.TryUpdateModelAsync(model,
                                                                              string.Empty,
                                                                              new ControllerContext(urlHelper.ActionContext),
                                                                              meta,
                                                                              modelfactory,
                                                                              new QueryStringValueProvider(BindingSource.Query, urlHelper.ActionContext.HttpContext.Request.Query, CultureInfo.CurrentCulture),
                                                                              validator);
            }

            foreach (var modifier in modifiers)
            {
                modifier(model);
            }

            var dictGenerator     = new RouteValueDictionaryGenerator();
            var actionConventions = urlHelper.ActionContext.HttpContext.RequestServices.GetRequiredService <ActionConventionOptions>();
            var dict = dictGenerator.Generate(model, (t, o) => actionConventions.TypeFormatters.ContainsKey(t) ? actionConventions.TypeFormatters[t](o, urlHelper.ActionContext) : o, (propertyInfo, atts) => actionConventions.PropertyNameModifier.GetModifiedPropertyName(propertyInfo, atts));
            var url  = urlHelper.RouteUrl(model.GetType().ToString(), dict);

            return(url);
        }
        public static async Task <string> ForAsync <T>(this IUrlHelper urlHelper, T model, bool bindExistingQueryString = false, params Action <T>[] modifiers) where T : class
        {
            await BindModelFor(urlHelper, model, bindExistingQueryString, modifiers);

            var dictGenerator     = new RouteValueDictionaryGenerator();
            var actionConventions = urlHelper.ActionContext.HttpContext.RequestServices.GetRequiredService <ActionConventionOptions>();
            var dict = dictGenerator.Generate(model, (t, o) => actionConventions.TypeFormatters.ContainsKey(t) ? actionConventions.TypeFormatters[t](o, urlHelper.ActionContext) : o, (propertyInfo, atts) => actionConventions.PropertyNameModifier.GetModifiedPropertyName(propertyInfo, atts));
            var url  = urlHelper.RouteUrl(model.GetType().ToString(), dict);

            return(url);
        }
示例#3
0
        public static async Task <string> ForAsync <T>(this IUrlHelper urlHelper, T model, bool bindExistingQueryString = false, params Action <T>[] modifiers) where T : class
        {
            await BindModelFor(urlHelper, model, bindExistingQueryString, modifiers);

            var dictGenerator     = new RouteValueDictionaryGenerator();
            var actionConventions = urlHelper.ActionContext.HttpContext.RequestServices.GetRequiredService <ActionConventionOptions>();
            var dict = dictGenerator.Generate(model, (t, o) => actionConventions.TypeFormatters.ContainsKey(t) ? actionConventions.TypeFormatters[t](o, urlHelper.ActionContext) : o, (propertyInfo, atts) => actionConventions.PropertyNameModifier.GetModifiedPropertyName(propertyInfo, atts));

            // Workaround for https://github.com/dotnet/aspnetcore/issues/14877
            var dict2 = dict.Concat(urlHelper.ActionContext.RouteData.Values
                                    .Where(x => x.Key != "action" && x.Key != "controller" && !dict.ContainsKey(x.Key)));

            var url = urlHelper.RouteUrl(model.GetType().ToString(), new RouteValueDictionary(dict2));

            return(url);
        }