问题
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;
}
Now, I can use it in Url.Action(..., ..., routeValues) helper and got something like this:
?str=val1&str=val2
etc.
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 ...
Thank you for your time.
EDIT:
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);
}
}
回答1:
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.
回答2:
You can use tag helpers like:
<a asp-area="" asp-controller="" asp-action="" asp-route-property1="value1" asp-route-property2="value2">my link</a>
and so on.
来源:https://stackoverflow.com/questions/42461590/asp-net-core-routing-with-routevaluedictionary