如何在 WebAPI 中使用哈希表作为条件检索数据?
How to Retrieve Data with Hashtable as a condition in WebAPI?
我知道如何在网络中用一个字符串检索api。但是我不知道如何在我的 webapi 中使用哈希表。因为在哈希表中,我对每种形式都有不同的条件,但调用相同的 api。
我正在使用.net3.5
示例:
System.Collections.Hashtable _condition = new System.Collections.Hashtable();
if (TPX.StringHelper.NVL(cbxStatus.EditValue, "-Please Select-") != "-Please Select-")
_condition.Add("Status", cbxStatus.EditValue.ToString());
if (TPX.StringHelper.NVL(cbxCenter.EditValue, "-Please Select-") != "-Please Select-")
_condition.Add("Center", cbxCenter.EditValue.ToString());
if (chkHideDone.Checked)
_condition.Add("IsDone", Convert.ToInt32(!chkHideDone.Checked));
_condition.Add("StartDate", dtpBegin.DateTime.ToString("yyyyMMdd"));
_condition.Add("EndDate", dtpEnd.DateTime.ToString("yyyyMMdd"));
string itemJson = Newtonsoft.Json.JsonConvert.SerializeObject(_condition);
string navURL = GlobalParameters.Host + "api/Order/RetrieveOrderList?conditions="+itemJson;
using (System.Net.WebClient webClient = new System.Net.WebClient())
{
webClient.Headers["Content-Type"] = "application/json";
webClient.Encoding = Encoding.UTF8;
string sJson = webClient.DownloadString(navURL); List<Models.OrderList> myDeserializedObjList = (List<Models.OrderList>)Newtonsoft.Json.JsonConvert.DeserializeObject(sJson, typeof(List<Models.OrderList>));
grdOrder.DataSource = myDeserializedObjList;
}
下载字符串 (webclient.downloadstring(navURL)) 时出错。
注意:我已经复制了相同的逻辑来创建一个新的并通过一个字段而不是哈希表进行过滤我没有问题。
我真的需要 hashtbale,因为我在项目的不同地方使用相同的逻辑,只是提供的条件不同。
非常感谢任何宝贵的意见。谢谢。
当您将 Hashtbale
序列化为 JSON 时,生成的字符串包含引号(可能还有其他特殊字符),它们在 URL querystring. These characters must be encoded using percent encoding. To do that, you can use Uri.EscapeDataString static 中不是有效字符方法。
string navURL = GlobalParameters.Host + "api/Order/RetrieveOrderList?conditions=" + Uri.EscapeDataString(itemJson);
编辑:
您可能会在 中找到一些如何将 Dictionary 传递给 WebAPI 的示例
- DefaultModelBinder cannot deserialize .NET Dictionary object passed to an action as a JSON object?
- How do I pass a Dictionary as a parameter to an ActionResult method from jQuery/Ajax?
但这对我有用:
// Declare binder that will convert parameters to Hashtable
public class HashtableBinder : IModelBinder
{
public bool BindModel(HttpActionContext actionContext, ModelBindingContext bindingContext)
{
var json = bindingContext?.ValueProvider?.GetValue(bindingContext.ModelName)?.AttemptedValue as string;
if (json != null)
{
//Deserialize using Microsoft's JSON serializer
var serializer = new JavaScriptSerializer();
bindingContext.Model = serializer.Deserialize<Hashtable>(json);
//or deserialize using Newtonsoft JSON convertor
//bindingContext.Model = JsonConvert.DeserializeObject<Hashtable>(json);
return true;
}
return false;
}
}
// In your controller, decorate parameter
// with FromUri attribute and specify HashtableBinder
// as BinderType
[HttpGet]
[Route("api/Order/RetrieveOrderList")]
public void RetrieveOrderList([FromUri(BinderType = typeof(HashtableBinder), Name = "conditions")]Hashtable conditions)
{
// access hashtable in conditions parameter, e.g. var status = conditions["Status"]
}
我知道如何在网络中用一个字符串检索api。但是我不知道如何在我的 webapi 中使用哈希表。因为在哈希表中,我对每种形式都有不同的条件,但调用相同的 api。
我正在使用.net3.5
示例:
System.Collections.Hashtable _condition = new System.Collections.Hashtable();
if (TPX.StringHelper.NVL(cbxStatus.EditValue, "-Please Select-") != "-Please Select-")
_condition.Add("Status", cbxStatus.EditValue.ToString());
if (TPX.StringHelper.NVL(cbxCenter.EditValue, "-Please Select-") != "-Please Select-")
_condition.Add("Center", cbxCenter.EditValue.ToString());
if (chkHideDone.Checked)
_condition.Add("IsDone", Convert.ToInt32(!chkHideDone.Checked));
_condition.Add("StartDate", dtpBegin.DateTime.ToString("yyyyMMdd"));
_condition.Add("EndDate", dtpEnd.DateTime.ToString("yyyyMMdd"));
string itemJson = Newtonsoft.Json.JsonConvert.SerializeObject(_condition);
string navURL = GlobalParameters.Host + "api/Order/RetrieveOrderList?conditions="+itemJson;
using (System.Net.WebClient webClient = new System.Net.WebClient())
{
webClient.Headers["Content-Type"] = "application/json";
webClient.Encoding = Encoding.UTF8;
string sJson = webClient.DownloadString(navURL); List<Models.OrderList> myDeserializedObjList = (List<Models.OrderList>)Newtonsoft.Json.JsonConvert.DeserializeObject(sJson, typeof(List<Models.OrderList>));
grdOrder.DataSource = myDeserializedObjList;
}
下载字符串 (webclient.downloadstring(navURL)) 时出错。 注意:我已经复制了相同的逻辑来创建一个新的并通过一个字段而不是哈希表进行过滤我没有问题。 我真的需要 hashtbale,因为我在项目的不同地方使用相同的逻辑,只是提供的条件不同。
非常感谢任何宝贵的意见。谢谢。
当您将 Hashtbale
序列化为 JSON 时,生成的字符串包含引号(可能还有其他特殊字符),它们在 URL querystring. These characters must be encoded using percent encoding. To do that, you can use Uri.EscapeDataString static 中不是有效字符方法。
string navURL = GlobalParameters.Host + "api/Order/RetrieveOrderList?conditions=" + Uri.EscapeDataString(itemJson);
编辑:
您可能会在 中找到一些如何将 Dictionary 传递给 WebAPI 的示例
- DefaultModelBinder cannot deserialize .NET Dictionary object passed to an action as a JSON object?
- How do I pass a Dictionary as a parameter to an ActionResult method from jQuery/Ajax?
但这对我有用:
// Declare binder that will convert parameters to Hashtable
public class HashtableBinder : IModelBinder
{
public bool BindModel(HttpActionContext actionContext, ModelBindingContext bindingContext)
{
var json = bindingContext?.ValueProvider?.GetValue(bindingContext.ModelName)?.AttemptedValue as string;
if (json != null)
{
//Deserialize using Microsoft's JSON serializer
var serializer = new JavaScriptSerializer();
bindingContext.Model = serializer.Deserialize<Hashtable>(json);
//or deserialize using Newtonsoft JSON convertor
//bindingContext.Model = JsonConvert.DeserializeObject<Hashtable>(json);
return true;
}
return false;
}
}
// In your controller, decorate parameter
// with FromUri attribute and specify HashtableBinder
// as BinderType
[HttpGet]
[Route("api/Order/RetrieveOrderList")]
public void RetrieveOrderList([FromUri(BinderType = typeof(HashtableBinder), Name = "conditions")]Hashtable conditions)
{
// access hashtable in conditions parameter, e.g. var status = conditions["Status"]
}