是否可以从查询字符串获取字典? [英] Is it possible to get Dictionary from query string?

查看:66
本文介绍了是否可以从查询字符串获取字典?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的控制器方法如下:

public ActionResult SomeMethod(Dictionary<int, string> model)
{

}

是否可以仅使用查询字符串来调用此方法并填充模型"?我的意思是,键入如下内容:

Is it possible to call this method and populate "model" using only query string? I mean, typing something like this:

ControllerName/SomeMethod?model.0=someText&model.1=someOtherText

在我们的浏览器地址栏中.有可能吗?

in our browser address bar. Is it possible?

似乎我的问题被误解了-我想绑定查询字符串,以便自动填充Dictionary方法参数.换句话说-我不想在我的方法中手动创建字典,但是我会用一些自动数学的.NET绑定器来创建它,所以我可以像这样直接访问它:

It would appear my question was misunderstood - I want to bind the query string, so that the Dictionary method parameter is populated automatically. In other words - I don't want to manually create the dictionary inside my method, but have some automathic .NET binder do it form me, so I can access it right away like this:

public ActionResult SomeMethod(Dictionary<int, string> model)
{
    var a = model[SomeKey];
}

有没有自动装订机,足够聪明地做到这一点?

Is there an automatic binder, smart enough to do this?

推荐答案

在ASP.NET Core中,可以使用以下语法(不需要自定义绑定程序):

In ASP.NET Core, you can use the following syntax (without needing a custom binder):

?dictionaryVariableName[KEY]=VALUE

假设您使用此方法作为您的方法:

Assuming you had this as your method:

public ActionResult SomeMethod([FromQuery] Dictionary<int, string> model)

然后调用以下URL:

?model[0]=firstString&model[1]=secondString

您的字典将被自动填充.带有值:

Your dictionary would then be automatically populated. With values:

(0, "firstString")
(1, "secondString")

这篇关于是否可以从查询字符串获取字典?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆