简体   繁体   English

使用路由在 QueryParameters 中传递字典

[英]Pass Dictionary in QueryParameters using Routing

I want to pass a dictionary through my controller using .Net Web Api.我想使用 .Net Web Api 通过我的控制器传递字典。

Unfortunately it isn't working as described at Microsoft Docs .不幸的是,它不像Microsoft Docs 中描述的那样工作。

My code looks the following:我的代码如下所示:

[HttpGet]
[Route("{key}")]
public ResponseType SearchObjects([FromUri] string key, 
    [FromUri]Dictionary<string, string> options) {}

The parameter 'index' works, but the parameter 'options' is always empty (length=0)...参数 'index' 有效,但参数 'options' 始终为空(长度 = 0)...

Requests I've tried so far:到目前为止我尝试过的请求:

http://localhost/key?index=1&filter[foo]=bar http://localhost/key?index=1&filter[foo]=bar

http://localhost/key?index=1&filter[ 'foo']=bar http://localhost/key?index=1&filter[ 'foo']=bar

http://localhost/key?index=1&filter.foo=bar http://localhost/key?index=1&filter.foo=bar

Anyone sees what I'm missing?有人看到我缺少什么吗? Are there any steps to debug/simulate the serialisation?是否有任何步骤来调试/模拟序列化?

You could use JSON!你可以使用 JSON! Just add the Newtonsoft.Json Package只需添加Newtonsoft.Json

Some thing like:就像是:

    [HttpGet("{json}")]
    public async Task<IActionResult> GetAsync([FromRoute] string json)
    {
        var dictionary = JsonConvert.DeserializeObject<IDictionary<string, string>>(json);
        return await Task.FromResult(Ok());
    }

EDIT:编辑:

Exemple of JSON: JSON 示例:

{ "Key1": "Value1", "Key2": "Value2", "Key3": "Value3" }

My solution now is a bit hacky but it seems that there isn't any better way at the moment...我现在的解决方案有点笨拙,但目前似乎没有更好的方法......

There is a Method parameter just as before.和以前一样,有一个 Method 参数。 To pass around the problem, that the Web Api not fills the dictionary automatically, I've implemented my own method... (If the Web Api does it in a later release, it will be simle to remove the method and everything should work then...为了解决这个问题,即 Web Api 不会自动填充字典,我已经实现了我自己的方法......(如果 Web Api 在以后的版本中执行它,删除该方法将很简单,一切都应该工作然后...

[HttpGet]
[Route("{key}")]
public ResponseType SearchObjects([FromUri] string key, [FromUri] Dictionary<string, string> options)
{
    AddMatchingQueryParamsToDictionary(Request, nameof(options), options);
    //options are now ready to use
}


/// <summary>
/// Fills the dictionary with additional values found in query parameters
/// In the query parameters given by the request, dictionary items will be in the format "dictName.dictKey".
/// </summary>
private static void AddMatchingQueryParamsToDictionary(HttpRequestMessage request, string dictionaryModelName, IDictionary<string, string> dictionaryToFill)
{
    var queryParams = request.GetQueryNameValuePairs();
    queryParams
        .Where(pair => pair.Key.StartsWith(dictionaryModelName))
        .Select(pair =>
        {
            var key = pair.Key;
            var index = key.IndexOf(dictionaryModelName, StringComparison.InvariantCultureIgnoreCase);
            var newKey = index < 0 ? key : key.Remove(index, dictionaryModelName.Length + 1 /*the dot*/);
            return (key: newKey, value: pair.Value);
        }).ToList()
        .ForEach(pair => dictionaryToFill[pair.key] = pair.value);
}

What do you think about this solution?您如何看待这个解决方案?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM