我正在使用 WCF 创建一个 ReSTful 服务。假设我的 OperationContract 是这样的:
[OperationContract]
[WebGet(
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Wrapped,
UriTemplate = "GetItemList/{token}/{value}"
)]
Stream GetItemList(string token);
因此,当我调用http://example.com/Service1.svc/GetItemList/myToken/myValue时,将调用该服务。
现在我想写一个默认方法说一些链接,'没有方法/端点存在',当用户调用 http://example.com/Service1.svc/GetItemList/myToken/或 http://example.com/ Service1.svc/GetItemList/myValue/或 http://example.com/Service1.svc/GetItemList/
如何在我的代码中实现它?
现在我正在做的是这样的:
[OperationContract]
[WebGet(
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Wrapped,
UriTemplate = "GetItemList/"
)]
string ValidateItemList();
[OperationContract]
[WebGet(
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Wrapped,
UriTemplate = "GetItemList/{Val1}"
)]
string ValidateItemList(string Val1);
在函数中,我只返回一个字符串“不存在方法”。
有没有更好的办法?我可以为定向到我的服务的 url 自定义错误消息吗?比如说,针对我的服务的所有非现有 url 请求的通用消息?