WCF/Rest/UriTemplate 变长查询字符串参数列表?

发布于 2024-09-26 05:49:22 字数 567 浏览 0 评论 0原文

WCF 将匹配以下内容:

http://localhost:8888/test/blahFirst/blahSecond/sdfsdf, wwewe

对此:

[OperationContract]
[WebGet( UriTemplate = "test/{first}/{second}/{val1},{val2}" )]
string GetVal( string first, string second, string val1, string val2 );

有没有办法使 va11、val2 成为可变长度参数列表?那么它可能是 val1, ...., valN?最终得到一个服务方法,例如:

string GetVal( string first, string second, List<string> params );

或者类似的东西?

WCF will match this:

http://localhost:8888/test/blahFirst/blahSecond/sdfsdf,wwewe

to this:

[OperationContract]
[WebGet( UriTemplate = "test/{first}/{second}/{val1},{val2}" )]
string GetVal( string first, string second, string val1, string val2 );

Is there a way to make the va11, val2 be a variable length list of parameters? So it could be val1, ...., valN? And end up with a service method such as:

string GetVal( string first, string second, List<string> params );

Or something along those lines?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

人间☆小暴躁 2024-10-03 05:49:22

只需获取一个简单的字符串,然后使用 split 方法将其转换为方法中的数组(或列表)即可。

您的界面应如下所示:

[OperationContract]
[WebGet(UriTemplate = "test/{first}/{second}/{val1}")]
string GetVal(string first, string second, string val1);

您的实现:

public string GetVal(string first, string second, string paramArray)
    {
        string[] parameters = paramArray.Split(',');

        foreach (string parameter in parameters)
        {
            Console.WriteLine(parameter);
        }

        return "Hello";
    }

并在浏览器中这样称呼它:

http://localhost:8731/MyServer/test/first/second/1,2,3

查看 MSDN 论坛以获得详细答案

Just GET a simple string and then convert it to an Array (or a list) in the method, using the split method.

Your Interface should look something like this:

[OperationContract]
[WebGet(UriTemplate = "test/{first}/{second}/{val1}")]
string GetVal(string first, string second, string val1);

Your implementation:

public string GetVal(string first, string second, string paramArray)
    {
        string[] parameters = paramArray.Split(',');

        foreach (string parameter in parameters)
        {
            Console.WriteLine(parameter);
        }

        return "Hello";
    }

And call it like this in your browser:

http://localhost:8731/MyServer/test/first/second/1,2,3

Take a look at the MSDN forum for a detailed answer

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文