使用RouteValueDictionary的ASP

使用RouteValueDictionary的ASP

本文介绍了使用RouteValueDictionary的ASP.NET Core路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要创建标签帮助程序以通过多选从表单生成网址。我记得,在较旧的mvc中,我可以这样使用:

I need to create Tag helper to generate url from form with multi select. I remember, in older mvc I could use something like this:

var i = 0;
foreach (var attribute in PaginatorFilterItems)
{
    foreach (var attributeFilter in attribute.Value)
    {
        routeValues.Add($"{attribute.Key}[{i++}]", attributeFilter.ToString());
    }
    i=0;
}

现在,我可以在Url.Action(...,。中使用它了。 ..,routeValues)助手,并得到以下类似信息:

Now, I can use it in Url.Action(..., ..., routeValues) helper and got something like this:

?str = val1& str = val2 等。

但是在dotnet核心中,我得到了一些转义序列。我不确定为什么:

But in dotnet core I get some escape sequence. I'm not sure why:

?str%5B0%5D=val1&str%5B1%5D=val2

[]被转义并且未绑定到路由。我该如何解决。我需要正确的收集格式:

[] are escaped and not binded to route. How can I fix it. I need correct collection format:

?str=val1&str=val2&str=val3 ...

谢谢您的时间。

编辑:

现在可以使用了。但是对于这个例子,我没有使用 RouteValueDictionary ,而是使用字符串列表和Join方法。

Now it is working. But for this example I didn't use RouteValueDictionary but string list and Join method.

public class PaginatorPageTagHelper : TagHelper
    {
        public IDictionary<string, ICollection> PaginatorFilterItems { get; set; }

        public string PaginatorController { get; set; }
        public string PaginatorAction { get; set; }
        public string PaginatorTitle { get; set; }
        public int PaginatorPageNumber { get; set; }
        public string PaginatorPageAttributeName { get; set; }
        public string PaginatorAttributeCategory { get; set; }

        public string PaginatorTagName { get; set; }
        public IUrlHelper PaginatorUrlHelper { get; set; }

        public override void Process(TagHelperContext context, TagHelperOutput output)
        {
            if(PaginatorUrlHelper == null) {
                throw new NullReferenceException("UrlHelper parameter cannot be null.");
            }

            var attributeList = new List<string>();
            var routeValues = new RouteValueDictionary();

            if (!string.IsNullOrEmpty(PaginatorAttributeCategory) && PaginatorFilterItems.ContainsKey(PaginatorAttributeCategory) && PaginatorFilterItems[PaginatorAttributeCategory].Count == 1)
            {
                routeValues.Add(PaginatorAttributeCategory, PaginatorFilterItems[PaginatorAttributeCategory].Cast<string>().First());
            }
            else
            {
                foreach (var attribute in PaginatorFilterItems)
                {
                    foreach (var attributeFilter in attribute.Value)
                    {
                        attributeList.Add($"{attribute.Key}={attributeFilter.ToString()}");
                    }
                }
            }

            //can be <a> ,<link> or custom
            output.TagName = PaginatorTagName ?? "a";
            output.Content.SetContent(PaginatorTitle);
            routeValues.Add("page", string.Empty);

            if (PaginatorPageNumber > 1)
            {
                routeValues["page"] = PaginatorPageNumber;
            }

            // create fallback for clasic filter, for example: /example?str=val1&str=val2
            if (attributeList.Any())
            {
                output.Attributes.SetAttribute("href", $"{this.GetPaginatorUrl(routeValues)}?{string.Join("&", attributeList)}");
                return;
            }

            output.Attributes.SetAttribute("href", this.GetPaginatorUrl(routeValues));
        }

        private string GetPaginatorUrl(RouteValueDictionary routeValues) {
            return PaginatorUrlHelper.Action(PaginatorAction, PaginatorController, routeValues);
        }
    }


推荐答案

I怀疑您的助手返回了一个MvcHtmlString,该MvcHtmlString在视图中呈现时将被编码。如果是这样,只需返回一个纯字符串即可。您需要确保手动对值进行URL编码,但在呈现时不会对其进行编码。

I suspect that your helper returns an MvcHtmlString, which will be encoded when it's rendered in the view. If so, simply return a plain string instead. You'll need to make sure that you URL encode your values manually but it won't be encoded when rendered.

这篇关于使用RouteValueDictionary的ASP.NET Core路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 00:47