如何根据参数名称前缀从URL查询绑定字典? [英] How to bind a Dictionary from URL query, based on parameter name prefix?

查看:63
本文介绍了如何根据参数名称前缀从URL查询绑定字典?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要处理 http://host.my/path?normal=1&othernormal=2&dict_a=b&dict_x=y

这是我最近的尝试,基于

我已经在MVC 1.1.0-preview1-final中对此进行了测试.

来自此处的文档: https://docs.microsoft.com/en-us/aspnet/core/mvc/models/model-binding

I need to handle http://host.my/path?normal=1&othernormal=2&dict_a=b&dict_x=y

Here's my latest attempt at it, based off How to use Bind Prefix? :

public string Get(int normal, int othernormal,
    [Bind(Prefix="dict_")]
    Dictionary<string, string> dict) { ... }

That creates a dictionary, however, it is empty.

It is important to note, that dict_a, and dict_x are not known in advance. E.g. dict_abracadabra=bla must be allowed.

解决方案

You're able to bind these values to a dictionary without a bind prefix. A dictionary will consume any simple values in the querystring.

Your sample query and method signature (without the bind prefix), and it seems to work pretty much as expected:

I've tested this in MVC 1.1.0-preview1-final.

From the docs here: https://docs.microsoft.com/en-us/aspnet/core/mvc/models/model-binding

这篇关于如何根据参数名称前缀从URL查询绑定字典?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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