.net 我可以使用 UriTemplate 将非字符串传递给 WCF RESTful 服务吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/545988/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 12:07:52  来源:igfitidea点击:

Can I pass non-string to WCF RESTful service using UriTemplate?

.netwcfweb-servicesrest

提问by Eugene Yokota

Can I do the following?

我可以执行以下操作吗?

[OperationContract]
[WebGet(UriTemplate = "/foo/{id}")]
string GetFoo(int id);

I'd like my service to function as both RESTful service and RPC-style SOAP service. If possible I'd like to retain int as int, and not do parsing by hand.

我希望我的服务既可以作为 RESTful 服务又可以作为 RPC 风格的 SOAP 服务。如果可能,我想将 int 保留为 int,而不是手动解析。

采纳答案by dthrasher

If I remember correctly, UriTemplate variables in the path always resolve to strings when using WebGet or WebInvoke. You can only bind UriTemplate variables to int, long, etc. when they are in the query portion of the UriTemplate.

如果我没记错的话,路径中的 UriTemplate 变量在使用 WebGet 或 WebInvoke 时总是解析为字符串。当 UriTemplate 变量位于 UriTemplate 的查询部分时,您只能将它们绑定到 int、long 等。

回答by Cameron Taggart

As dthrasher mentioned, move id to the query part of the URI. This worked for me:

正如 dthrasher 所提到的,将 id 移动到 URI 的查询部分。这对我有用:

[OperationContract]
[WebGet(UriTemplate = "/foo?id={id}")]
string GetFoo(int id);

See "URI scheme" on wikipedia for more info about the different parts of a URI: http://en.wikipedia.org/wiki/URI_scheme

有关 URI 不同部分的更多信息,请参阅维基百科上的“URI 方案”:http: //en.wikipedia.org/wiki/URI_scheme

回答by Ohad Schneider

As others mentioned, you must use query strings in order to pass non-string parameters. The following article details how the parsing is done.

正如其他人提到的,您必须使用查询字符串才能传递非字符串参数。下面的文章详细介绍了解析是如何完成的。

WCF Extensibility – QueryStringConverter

WCF 可扩展性 – QueryStringConverter

Coming back to “proper” WCF extensibility, this week's post is about the QueryStringConverter. This is actually a simple topic to be covered, as its purpose is quite specific (unlike other extensibility points seen before, which could be used for a wide variety of cases) – within WCF the QueryStringConverter is only used on endpoints which have the WebHttpBehavior applied to them. And even in those, only on operations which have parameters passed via the query strings(either operations with parameters marked with [WebGet] or a [WebInvoke] operation with a UriTemplate that explicitly binds some parameters to the query string). A QueryStringConverter is the piece which can convert between operation parameters and their representation in a query string.

...

The default QueryStringConverter used by the WebHttpBehavior supports natively several types, including all simple numeric types (Byte, SByte, Int16, Int32, Int64, UInt16, UInt32, UInt64, Single, Double, Decimal), Boolean, Char, Object, String, DateTime, DateTimeOffset, TimeSpan, Guid, Uri, and arrays of Byte(essentially, all the types which the DataContractSerializer considers to be “primitives”, with the exception of XmlQualifiedName). Enumeration types are also supported by default(the string representation of the enum values are used). Finally, there is also another set of types which are supported by the default QueryStringConverter – any one which declares a [TypeConverter] attribute with a type converter which can convert the type to and from strings (more on that below).

回到“正确的”WCF 可扩展性,本周的帖子是关于 QueryStringConverter。这实际上是一个需要涵盖的简单主题,因为它的目的非常具体(与之前看到的其他扩展点不同,后者可用于多种情况)——在 WCF 中,QueryStringConverter 仅用于应用了 WebHttpBehavior 的端点给他们。即使在这些操作中,也仅适用于通过查询字符串传递参数的操作(带有标记为 [WebGet] 的参数的操作或带有将某些参数显式绑定到查询字符串的 UriTemplate 的 [WebInvoke] 操作)。QueryStringConverter 是可以在操作参数及其在查询字符串中的表示之间进行转换的部分。

...

WebHttpBehavior 使用的默认 QueryStringConverter 原生支持多种类型,包括所有简单的数字类型(Byte、SByte、Int16、Int32、Int64、UInt16、UInt32、UInt64、Single、Double、Decimal)、Boolean、Char、Object、String、DateTime 、DateTimeOffset、TimeSpan、Guid、Uri 和 Byte 数组(基本上,DataContractSerializer 认为是“原始类型”的所有类型,XmlQualifiedName 除外)。默认情况下也支持枚举类型(使用枚举值的字符串表示)。最后,还有另一组默认 QueryStringConverter 支持的类型 - 任何声明 [TypeConverter] 属性的类型转换器,该类型转换器可以将类型转换为字符串或从字符串转换(更多内容见下文)。

回答by Andrew Hare

Unfortunately you must do the parsing yourself if you want to use the UriTemplate.

不幸的是,如果您想使用UriTemplate.