我需要對包含Dictionary作爲參數的方法發出GET請求。我通過瀏覽,但無法找到任何有關如何發送字典的信息,所以我的請求打到我的方法。方法簽名是像下面使用Asp.Net Web API中的字典參數的方法
public void AddItems(Dictionary<string,object> Items)
最好的問候,
末爾
我需要對包含Dictionary作爲參數的方法發出GET請求。我通過瀏覽,但無法找到任何有關如何發送字典的信息,所以我的請求打到我的方法。方法簽名是像下面使用Asp.Net Web API中的字典參數的方法
public void AddItems(Dictionary<string,object> Items)
最好的問候,
末爾
謝謝!這是一個明確的職位,回答我的問題:) – kkocabiyik 2012-08-14 10:42:20
這篇文章是爲MVC,這是如何工作的Web API? – 2012-08-14 10:54:30
Asp。 Net Web Api是asp.net mvc的一部分 – 2012-08-14 11:02:48
您可以使用字典作爲參數,以這樣的方式
protected object DictionaryFunction()
{
Dictionary<int,YourObjectName> YourDictionaryObjectName=new Dictionary<int,YourObjectName>();
...
...
return YourDictionaryObjectName;
}
protected MyFunction()
{
Dictionary<int,YourObjectName> MyDictionary=(Dictionary<int,YourObjectName>)DictionaryFunction();
}
我寫了一個ModelBinder,它完全符合你的要求:
public class DictionaryModelBinder : DefaultModelBinder
{
private const string _dateTimeFormat = "dd/MM/yyyy HH:mm:ss";
private enum StateMachine
{
NewSection,
Key,
Delimiter,
Value,
ValueArray
}
public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
var stream = controllerContext.HttpContext.Request.InputStream;
string text;
stream.Position = 0;
using (var reader = new StreamReader(stream))
{
text = reader.ReadToEnd();
}
int index = 0;
return Build(text, ref index);
}
private static Dictionary<string, object> Build(string text, ref int index)
{
var state = StateMachine.NewSection;
var dictionary = new Dictionary<string, object>();
var key = string.Empty;
object value = string.Empty;
for (; index < text.Length; ++index)
{
if (state == StateMachine.NewSection && text[index] == '{')
{
dictionary = new Dictionary<string, object>();
state = StateMachine.NewSection;
}
else if (state == StateMachine.NewSection && text[index] == '"')
{
key = string.Empty;
state = StateMachine.Key;
}
else if (state == StateMachine.Key && text[index] != '"')
{
key += text[index];
}
else if (state == StateMachine.Key && text[index] == '"')
{
state = StateMachine.Delimiter;
}
else if (state == StateMachine.Delimiter && text[index] == ':')
{
state = StateMachine.Value;
value = string.Empty;
}
else if (state == StateMachine.Value && text[index] == '[')
{
state = StateMachine.ValueArray;
value = value.ToString() + text[index];
}
else if (state == StateMachine.ValueArray && text[index] == ']')
{
state = StateMachine.Value;
value = value.ToString() + text[index];
}
else if (state == StateMachine.Value && text[index] == '{')
{
value = Build(text, ref index);
}
else if (state == StateMachine.Value && text[index] == ',')
{
dictionary.Add(key, ConvertValue(value));
state = StateMachine.NewSection;
}
else if (state == StateMachine.Value && text[index] == '}')
{
dictionary.Add(key, ConvertValue(value));
return dictionary;
}
else if (state == StateMachine.Value || state == StateMachine.ValueArray)
{
value = value.ToString() + text[index];
}
}
return dictionary;
}
private static object ConvertValue(object value)
{
string valueStr;
if (value is Dictionary<string, object> || value == null || (valueStr = value.ToString()).Length == 0)
{
return value;
}
bool boolValue;
if (bool.TryParse(valueStr, out boolValue))
{
return boolValue;
}
int intValue;
if (int.TryParse(valueStr, out intValue))
{
return intValue;
}
double doubleValue;
if (double.TryParse(valueStr, out doubleValue))
{
return doubleValue;
}
valueStr = valueStr.Trim('"');
DateTime datetimeValue;
if (DateTime.TryParseExact(valueStr, _dateTimeFormat, CultureInfo.InvariantCulture, DateTimeStyles.None, out datetimeValue))
{
return datetimeValue;
}
if (valueStr.First() == '[' && valueStr.Last() == ']')
{
valueStr = valueStr.Trim('[', ']');
if (valueStr.Length > 0)
{
if (valueStr[0] == '"')
{
return valueStr
.Split(new[] { '"' }, StringSplitOptions.RemoveEmptyEntries)
.Where(x => x != ",")
.ToArray();
}
else
{
return valueStr
.Split(',')
.Select(x => ConvertValue(x.Trim()))
.ToArray();
}
}
}
return valueStr;
}
}
更多的解釋和全文後,你可以在我的博客看看:
@kkcocabiyik是你無法對項目進行傳遞到您的GET請求的URL查詢參數?如果是這樣,你可以發佈一個語法樣本來使用嗎?謝謝! – msplants 2013-10-08 18:19:37