使用RouteValueDictionary的ASP.NET核心路由

时间:2017-02-25 21:07:10

标签: asp.net-mvc routing asp.net-core asp.net-core-mvc

我需要创建Tag帮助器以从多重选择的表单生成url。我记得,在较老的mvc中,我可以使用这样的东西:

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)帮助器中使用它,并得到类似这样的内容:

?str=val1&str=val2等。

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

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

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

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

感谢您的时间。

修改

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

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);
        }
    }

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

您可以使用以下标记帮助:

... /* Define the data range including headers */ AreaReference my_data_range = new AreaReference(new CellReference(rowStart, columnStart), new CellReference(areaRow, areaColumn)); /* Set Range to the Table */ String wantedDisplayName = "MYTABLE"; String wantedName = "Test1"; long id = 0L; java.util.List<XSSFTable> all_tables = sheet.getTables(); for (XSSFTable a_table : all_tables) { if (wantedDisplayName.equals(a_table.getDisplayName())) wantedDisplayName += "_1"; if (wantedName.equals(a_table.getName())) wantedName += "_1"; if (a_table.getCTTable().getId() > id) id = a_table.getCTTable().getId(); System.out.println(wantedDisplayName); System.out.println(wantedName); System.out.println(id); } id++; cttable.setRef(my_data_range.formatAsString()); cttable.setDisplayName(wantedDisplayName); /* this is the display name of the table */ cttable.setName(wantedName); /* This maps to "displayName" attribute in <table>, OOXML */ cttable.setId(id); //id attribute against table as long value ... 等等。