object IHttpParameterContainer.GetValue(HttpParamLocation location, string name) { if (!_isInitialized) { Initialize(); } return(GetValue(location, name)); }
IEnumerable IHttpParameterContainer.GetValues(HttpParamLocation location, string name) { if (!_isInitialized) { Initialize(); } return(Get(location, name).Values); }
/// <summary> /// 根据参数新建一个key /// </summary> /// <param name="name"> 参数名 </param> /// <param name="location"> 参数位置 </param> /// <returns> </returns> private string CreateKey(string name, HttpParamLocation location) { if (name == null) { return($"{location}(null)"); } return($"{location} | {name.ToLowerInvariant()}"); }
bool IHttpParameterContainer.Contains(HttpParamLocation location, string name) { if (!_isInitialized) { Initialize(); } return(Contains(location, name)); }
/// <summary> /// 初始化 Http参数集合 /// </summary> /// <param name="params"> 参数容器 </param> /// <param name="location"> </param> /// <exception cref="ArgumentNullException"> <paramref name="params" /> is null </exception> protected HttpParamsBase(IHttpParameterContainer @params, HttpParamLocation location) { if (@params == null) { throw new ArgumentNullException(nameof(@params)); } Params = @params; Location = location; }
/// <summary> /// 获取<seealso cref="HttpParamValue" /> /// </summary> /// <param name="location"> 参数位置 </param> /// <param name="name"> 参数名 </param> /// <returns> </returns> public HttpParamValue Get(HttpParamLocation location, string name) { var key = CreateKey(name, location); var value = BaseGet(key); if (value == null) { return(new HttpParamValue(name, null, location)); } return((HttpParamValue)value); }
void IHttpParameterContainer.Remove(HttpParamLocation location, string name) { if (location == HttpParamLocation.Body) { throw new NotSupportedException("集合为只读"); } if (!_isInitialized) { Initialize(); } Remove(location, name); }
void IHttpParameterContainer.AddValue(HttpParamLocation location, string name, object value) { if (location == HttpParamLocation.Body) { throw new NotSupportedException("集合为只读"); } if (!_isInitialized) { Initialize(); } AddValue(location, name, value); }
/// <summary> /// 添加参数 /// </summary> /// <param name="location"> 参数位置 </param> /// <param name="name"> 参数名称 </param> /// <param name="value"> 参数值 </param> public void AddValue(HttpParamLocation location, string name, object value) { if (value == null) { return; } var key = CreateKey(name, location); var param = (HttpParamValue)BaseGet(key); if (param == null) //如果指定名称的参数不存在,直接添加 { BaseSet(key, new HttpParamValue(name, value, location)); } else { param.AddValue(value); } }
/// <summary> /// 设置参数值 /// </summary> /// <param name="location"> 参数位置 </param> /// <param name="name"> 参数名 </param> /// <param name="value"> 参数值 </param> public void SetValue(HttpParamLocation location, string name, object value) { var key = CreateKey(name, location); if (value == null) { BaseRemove(key); } else { var old = (HttpParamValue)BaseGet(key); if (old == null) { BaseSet(key, new HttpParamValue(name, value, location)); } else { old.SetValue(value); } } }
/// <summary> /// 移除指定名称的参数 /// </summary> /// <param name="location"> 待移除的参数位置 </param> /// <param name="name"> 待移除的参数名称 </param> public void Remove(HttpParamLocation location, string name) => BaseRemove(CreateKey(name, location));
internal HttpStringParams(IHttpParameterContainer @params, HttpParamLocation location) : base(@params, location) { }
/// <summary> /// 获取参数值 /// </summary> /// <param name="location"> 参数位置 </param> /// <param name="name"> 参数名 </param> /// <returns> </returns> public IEnumerable GetValues(HttpParamLocation location, string name) => Get(location, name).Values;
/// <summary> /// 获取参数值,如果指定名称的参数有多个值,只返回一个匹配的参数值 /// </summary> /// <param name="location"> 参数位置 </param> /// <param name="name"> 参数名 </param> /// <returns> </returns> public object GetValue(HttpParamLocation location, string name) => Get(location, name).FirstValue;
/// <summary> /// 初始化参数 /// </summary> /// <param name="name"> 参数名 </param> /// <param name="value"> 参数值 </param> /// <param name="location"> 参数位置 </param> public HttpParamValue(string name, object value, HttpParamLocation location) { Name = name; Location = location; SetValue(value); }
/// <summary> /// 判断指定名称的参数是否存在 /// </summary> /// <param name="location"> 参数位置 </param> /// <param name="name"> 参数名 </param> /// <returns> </returns> public bool Contains(HttpParamLocation location, string name) => BaseGet(CreateKey(name, location)) != null;
public HttpQuery(IHttpParameterContainer @params, HttpParamLocation location) : base(@params, location) { ArrayEncodeMode = ArrayEncodeMode.Default; ObjectEncodeMode = ObjectEncodeMode.Default; }
/// <summary> /// 设置参数 /// </summary> /// <param name="name"> 参数名 </param> /// <param name="value"> 参数值 </param> /// <param name="location"> 参数位置 </param> protected void SetParam(string name, object value, HttpParamLocation location) => _paramContainer.SetValue(location, name, value);