加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-mvc – 如何创建ASP.NET MVC控制器从查询字符串接受无限

发布时间:2020-12-16 06:25:08 所属栏目:asp.Net 来源:网络整理
导读:URL的示例 ????HTTP _ //主机/ URL /无限/指数首次=值1放大器;第二=数值…放大器; anyvalidname = someValue中 我希望有一个行动接受未知数量的未知名称的参数.像这样的东西: public class UnlimitedController : Controller{ public ActionResult Index(ob
URL的示例
????HTTP _ //主机/ URL /无限/指数首次=值1&放大器;第二=数值…&放大器; anyvalidname = someValue中

我希望有一个行动接受未知数量的未知名称的参数.像这样的东西:

public class UnlimitedController : Controller
{
    public ActionResult Index(object queryParams)
    {

    }

    //or even better
    public ActionResult Index(Dictionary<string,object> queryParams)
    {

    }
}

解决方法

您可以创建一个自定义模型绑定器,将查询字符串转换为字典.

自定义模型粘合剂

public class CustomModelBinder: IModelBinder
  {
    public object BindModel(ControllerContext controllerContext,ModelBindingContext bindingContext)
    {
      var querystrings = controllerContext.HttpContext.Request.QueryString;

      return querystrings.Cast<string>()
        .Select(s => new { Key = s,Value = querystrings[s] })  
        .ToDictionary(p => p.Key,p => p.Value);
    }
  }

行动

public ActionResult Index([ModelBinder(typeof(CustomModelBinder))]
   Dictionary<string,string> queryParams)
{

}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读