本文整理汇总了C#中QueryBuilder.ToString方法的典型用法代码示例。如果您正苦于以下问题:C# QueryBuilder.ToString方法的具体用法?C# QueryBuilder.ToString怎么用?C# QueryBuilder.ToString使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类QueryBuilder
的用法示例。
在下文中一共展示了QueryBuilder.ToString方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: AddMultipleValues_AddedInOrder
public void AddMultipleValues_AddedInOrder()
{
var builder = new QueryBuilder();
builder.Add("key1", "value1");
builder.Add("key2", "value2");
builder.Add("key3", "value3");
Assert.Equal("?key1=value1&key2=value2&key3=value3", builder.ToString());
}
示例2: AddMultipleValuesViaInitializer_AddedInOrder
public void AddMultipleValuesViaInitializer_AddedInOrder()
{
var builder = new QueryBuilder()
{
{ "key1", "value1" },
{ "key2", "value2" },
{ "key3", "value3" },
};
Assert.Equal("?key1=value1&key2=value2&key3=value3", builder.ToString());
}
示例3: AddMultipleValuesViaConstructor_AddedInOrder
public void AddMultipleValuesViaConstructor_AddedInOrder()
{
var builder = new QueryBuilder(new[]
{
new KeyValuePair<string, string>("key1", "value1"),
new KeyValuePair<string, string>("key2", "value2"),
new KeyValuePair<string, string>("key3", "value3"),
});
Assert.Equal("?key1=value1&key2=value2&key3=value3", builder.ToString());
}
示例4: ChangeQueryParameterValue
protected static string ChangeQueryParameterValue(string baseUri, string queryParameterName,
string queryParameterValue)
{
// Hosted uri for API compatibility
var hostedUri = new Uri("http://test.local/");
// Generate uri object
var uri = new Uri(baseUri, UriKind.RelativeOrAbsolute);
// Start with slash detection.
var isStartWithSlash = false;
// If the uri is not absolute uri, convert it to an absolute one
var isAbsolute = uri.IsAbsoluteUri;
if (!isAbsolute)
{
isStartWithSlash = !string.IsNullOrEmpty(baseUri) && baseUri[0] == '/';
uri = new Uri(hostedUri, uri);
}
// Extract query string and change parameter
var qs = QueryHelpers.ParseQuery(uri.Query);
qs[queryParameterName] = new[] { queryParameterValue };
var qb = new QueryBuilder();
foreach (var item in qs)
{
qb.Add(item.Key, (IEnumerable<string>)item.Value);
}
// Rebuild uri
var builder = new UriBuilder(uri)
{
Query = qb.ToString().Remove(0, 1) // Remove leading "?" mark
};
var finalUri = builder.Uri;
// If the original uri is not absolute, remove the hosted part
if (!isAbsolute)
{
finalUri = hostedUri.MakeRelativeUri(finalUri);
}
var result = UriHelper.Encode(finalUri);
// Add start slash if necessary
if (!isAbsolute && isStartWithSlash)
{
result = "/" + result;
}
return result;
}
示例5: CopyViaConstructor_AddedInOrder
public void CopyViaConstructor_AddedInOrder()
{
var builder = new QueryBuilder()
{
{ "key1", "value1" },
{ "key2", "value2" },
{ "key3", "value3" },
};
var builder1 = new QueryBuilder(builder);
Assert.Equal("?key1=value1&key2=value2&key3=value3", builder1.ToString());
}
示例6: AddIEnumerableValues_AddedInOrder
public void AddIEnumerableValues_AddedInOrder()
{
var builder = new QueryBuilder();
builder.Add("key", new[] { "value1", "value2", "value3" });
Assert.Equal("?key=value1&key=value2&key=value3", builder.ToString());
}
示例7: AddReservedCharacters_PercentEncoded
public void AddReservedCharacters_PercentEncoded()
{
var builder = new QueryBuilder();
builder.Add("key&", "value#");
Assert.Equal("?key%26=value%23", builder.ToString());
}
示例8: AddSpace_PercentEncoded
public void AddSpace_PercentEncoded()
{
var builder = new QueryBuilder();
builder.Add("key", "value 1");
Assert.Equal("?key=value%201", builder.ToString());
}
示例9: AddSimple_NoEncoding
public void AddSimple_NoEncoding()
{
var builder = new QueryBuilder();
builder.Add("key", "value");
Assert.Equal("?key=value", builder.ToString());
}
示例10: EmptyQuery_NoQuestionMark
public void EmptyQuery_NoQuestionMark()
{
var builder = new QueryBuilder();
Assert.Equal(string.Empty, builder.ToString());
}
示例11: BindValues
// Step 2: If the route is a match generate the appropriate URI
public string BindValues(IDictionary<string, object> acceptedValues)
{
var context = new UriBuildingContext();
for (var i = 0; i < _template.Segments.Count; i++)
{
Debug.Assert(context.BufferState == SegmentState.Beginning);
Debug.Assert(context.UriState == SegmentState.Beginning);
var segment = _template.Segments[i];
for (var j = 0; j < segment.Parts.Count; j++)
{
var part = segment.Parts[j];
if (part.IsLiteral)
{
if (!context.Accept(part.Text))
{
return null;
}
}
else if (part.IsParameter)
{
// If it's a parameter, get its value
object value;
var hasValue = acceptedValues.TryGetValue(part.Name, out value);
if (hasValue)
{
acceptedValues.Remove(part.Name);
}
var isSameAsDefault = false;
object defaultValue;
if (_defaults != null && _defaults.TryGetValue(part.Name, out defaultValue))
{
if (RoutePartsEqual(value, defaultValue))
{
isSameAsDefault = true;
}
}
var converted = Convert.ToString(value, CultureInfo.InvariantCulture);
if (isSameAsDefault)
{
// If the accepted value is the same as the default value buffer it since
// we won't necessarily add it to the URI we generate.
if (!context.Buffer(converted))
{
return null;
}
}
else
{
// If the value is not accepted, it is null or empty value in the
// middle of the segment. We accept this if the parameter is an
// optional parameter and it is preceded by an optional seperator.
// I this case, we need to remove the optional seperator that we
// have added to the URI
// Example: template = {id}.{format?}. parameters: id=5
// In this case after we have generated "5.", we wont find any value
// for format, so we remove '.' and generate 5.
if (!context.Accept(converted))
{
if (j != 0 && part.IsOptional && segment.Parts[j - 1].IsOptionalSeperator)
{
context.Remove(segment.Parts[j - 1].Text);
}
else
{
return null;
}
}
}
}
}
context.EndSegment();
}
// Encode the URI before we append the query string, otherwise we would double encode the query string
var encoded = new StringBuilder();
encoded.Append(UriEncode(context.Build()));
// Generate the query string from the remaining values
var queryBuilder = new QueryBuilder();
foreach (var kvp in acceptedValues)
{
if (_defaults != null && _defaults.ContainsKey(kvp.Key))
{
// This value is a 'filter' we don't need to put it in the query string.
continue;
}
var converted = Convert.ToString(kvp.Value, CultureInfo.InvariantCulture);
if (String.IsNullOrEmpty(converted))
{
continue;
}
//.........这里部分代码省略.........