Browse Source

Merge pull request #7309 from abpframework/maliming/AddQueryStringParameter

Handle empty array in AddQueryStringParameter.
pull/7355/head
Halil İbrahim Kalkan 6 years ago
committed by GitHub
parent
commit
6117e2b362
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 35
      framework/src/Volo.Abp.Http.Client/Volo/Abp/Http/Client/DynamicProxying/UrlBuilder.cs
  2. 9
      framework/test/Volo.Abp.Http.Client.Tests/Volo/Abp/Http/DynamicProxying/RegularTestController.cs

35
framework/src/Volo.Abp.Http.Client/Volo/Abp/Http/Client/DynamicProxying/UrlBuilder.cs

@ -8,7 +8,6 @@ using JetBrains.Annotations;
using Volo.Abp.Http.Modeling; using Volo.Abp.Http.Modeling;
using Volo.Abp.Http.ProxyScripting.Generators; using Volo.Abp.Http.ProxyScripting.Generators;
using Volo.Abp.Localization; using Volo.Abp.Localization;
using Volo.Abp.Reflection;
namespace Volo.Abp.Http.Client.DynamicProxying namespace Volo.Abp.Http.Client.DynamicProxying
{ {
@ -82,9 +81,10 @@ namespace Volo.Abp.Http.Client.DynamicProxying
continue; continue;
} }
AddQueryStringParameter(urlBuilder, isFirstParam, queryStringParameter.Name, value); if (AddQueryStringParameter(urlBuilder, isFirstParam, queryStringParameter.Name, value))
{
isFirstParam = false; isFirstParam = false;
}
} }
if (apiVersion.ShouldSendInQueryString()) if (apiVersion.ShouldSendInQueryString())
@ -93,28 +93,37 @@ namespace Volo.Abp.Http.Client.DynamicProxying
} }
} }
private static void AddQueryStringParameter( private static bool AddQueryStringParameter(
StringBuilder urlBuilder, StringBuilder urlBuilder,
bool isFirstParam, bool isFirstParam,
string name, string name,
[NotNull] object value) [NotNull] object value)
{ {
urlBuilder.Append(isFirstParam ? "?" : "&");
if (value.GetType().IsArray || (value.GetType().IsGenericType && value is IEnumerable)) if (value.GetType().IsArray || (value.GetType().IsGenericType && value is IEnumerable))
{ {
var index = 0; var index = 0;
foreach (var item in (IEnumerable) value) foreach (var item in (IEnumerable) value)
{ {
if (index == 0)
{
urlBuilder.Append(isFirstParam ? "?" : "&");
}
urlBuilder.Append(name + $"[{index++}]=" + System.Net.WebUtility.UrlEncode(ConvertValueToString(item)) + "&"); urlBuilder.Append(name + $"[{index++}]=" + System.Net.WebUtility.UrlEncode(ConvertValueToString(item)) + "&");
} }
//remove & at the end of the urlBuilder.
urlBuilder.Remove(urlBuilder.Length - 1, 1); if (index > 0)
} {
else //remove & at the end of the urlBuilder.
{ urlBuilder.Remove(urlBuilder.Length - 1, 1);
urlBuilder.Append(name + "=" + System.Net.WebUtility.UrlEncode(ConvertValueToString(value))); return true;
}
return false;
} }
urlBuilder.Append(isFirstParam ? "?" : "&");
urlBuilder.Append(name + "=" + System.Net.WebUtility.UrlEncode(ConvertValueToString(value)));
return true;
} }
private static string ConvertValueToString([NotNull] object value) private static string ConvertValueToString([NotNull] object value)

9
framework/test/Volo.Abp.Http.Client.Tests/Volo/Abp/Http/DynamicProxying/RegularTestController.cs

@ -1,4 +1,5 @@
using System; using System;
using System.Collections.Generic;
using System.Globalization; using System.Globalization;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
@ -140,5 +141,13 @@ namespace Volo.Abp.Http.DynamicProxying
[FromQuery] [FromQuery]
public DateTime FirstReleaseDate { get; set; } public DateTime FirstReleaseDate { get; set; }
[FromQuery]
public List<string> Colors { get; set; }
public Car()
{
Colors = new List<string>();
}
} }
} }

Loading…
Cancel
Save