在 WCF 中,我可以在 UriTemplate 的文字段中使用通配符吗? [英] In WCF can I have a wildcard character in a literal segment of my UriTemplate?

查看:112
本文介绍了在 WCF 中,我可以在 UriTemplate 的文字段中使用通配符吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 .Net 4.0 创作 RESTful WCF 服务.我想要以下两个网址:

I am authoring a RESTful WCF service using .Net 4.0. I want the following two URLS:

/root/document/{ids}?fields={fields}
/root/externaldocument/{ids}?fields={fields}

映射到同一个接口成员:

to map to the same interface member:

Documents GetDocuments(string ids, string fields)

我尝试将通配符放入文字 URL 段:

I have tried putting a wildcard into a literal URL segment:

    [OperationContract]
    [WebGet(UriTemplate = "/root/*document/{ids}?fields={fields}")]
    Documents GetDocuments(string ids, string fields)

但是,这是无效的,我收到以下异常:

However, this is not valid and I get the following exception:

The UriTemplate '/root/*document/{ids}?fields={fields}' is not valid; the 
wildcard ('*') cannot appear in a variable name or literal... Note that a 
wildcard segment, either a literal or a variable, is valid only as the last 
path segment in the template

如果我将通配符段包裹在模板大括号中:

If I wrap the wildcard segment in a template brace:

    [OperationContract]
    [WebGet(UriTemplate = "/root/{*document}/{ids}?fields={fields}")]
    Documents GetDocuments(string ids, string fields)

然后我得到一个异常,因为方法参数中没有这样的输入参数:

Then I get an exception because there is no such input parameter in the method arguments:

Operation 'GetDocuments' in contract 'IAPIv2' has a UriTemplate that expects a 
parameter named 'DOCUMENTS', but there is no input parameter with that name 
on the operation.

我的解决方法是让两个条目指向不同的方法,然后让这些方法调用一个通用实现:

My workaround is simply to have two entries, pointing to different methods, and then have the methods call a common implementation:

    [OperationContract]
    [WebGet(UriTemplate = "/root/document/{ids}?fields={fields}")]
    Documents GetDocuments(string ids, string fields)

    [OperationContract]
    [WebGet(UriTemplate = "/root/externaldocument/{ids}?fields={fields}")]
     Documents GetExternalDocuments(string ids, string fields)

但这看起来有点难看.

我已经阅读了文档,但找不到这点的具体地址.有什么办法可以在 WCF 中使用通配符文字段吗?或者这在 WCF 中是不可能的?

I have read the documentation and cannot find this point specifically address. Is there any way I can have a wildcard literal segment in WCF? Or is this not possible in WCF?

推荐答案

事实证明,这两个入口点需要具有略微不同的功能.所以我需要捕获用于输入方法的 URL.我最终做的是以下内容:

As it turned out, the two entry points needed to have slightly different functionality. So I needed to capture which URL was used to enter the method. What I ended up doing was the following:

[OperationContract]
[WebGet(UriTemplate = "/root/{source}ocuments/{ids}?fields={fields}")]
DocumentCollection GetDocumentsById(string source, string ids, string fields);

两个网址:

/root/document/{ids}?fields={fields}
/root/externaldocument/{ids}?fields={fields}

映射到相同的 URL 模板,因此我只需要在我的界面中有一个带有单个 UriTemplate 的条目.

map to the same URL template, and thus I needed to have only a single entry with a single UriTemplate in my interface.

如果第二段是documents",source"输入参数捕获d",如果第二段是externaldocuments",则捕获externald".因此,通过检查这个输入参数,该方法可以做出适当的反应,具体取决于哪个URL 用于访问该方法.

The "source" input parameter captures either "d" if the second segment is "documents or "externald" if the second segment is "externaldocuments". Thus by inspecting this input parameter, the method can react appropriately, depending upon which URL was used to reach the method.

请注意,我不能对 UriTemplate 使用以下内容:

Note that I could not use the following for the UriTemplate:

[WebGet(UriTemplate = "/root/{source}documents/{ids}?fields={fields}")]

因为在这种情况下,传入的 URL

because in this case, the incoming URL

/root/document/{ids}?fields={fields}

将不匹配模板,即使模板匹配,如果源输入参数使用空字符串 ("").显然,UriTemplate 匹配算法要求参数捕获组中至少有一个字符才能进行匹配.

would not match the template, even though the template matches if an empty string ("") is used for the source input parameter. Apparently the UriTemplate matching algorithm requires there to be at least one character in a parameter capturing group for there to be a match.

这篇关于在 WCF 中,我可以在 UriTemplate 的文字段中使用通配符吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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