【发布时间】:2012-05-17 06:11:33
【问题描述】:
由于我是 WCF 新手,并且在 IIS 中的虚拟目录 Api(网址类似于 http://localhost/api/taskapi.svc)中配置了 WCF 服务端点,我一直在寻找通过 Web 浏览器发出请求的方法http://localhost/api/taskapi.svc/GetCompleted 之类的东西会以 JSON 响应,列出所有已完成的任务,因此这里的这两个帖子给了我一些答案
好的,嗯,所以我将我的 OperationContract 更改为如下所示
[OperationContract]
[WebGet(UriTemplate = "/GetCompleted", ResponseFormat = WebMessageFormat.Json)]
IList<Task> GetCompleted();
但浏览器中的 url http://localhost/api/tasksapi.svc/GetCompleted 仍然以 400 Bad Request 响应。
服务合同
[ServiceContract]
public interface ITaskContract
{
[OperationContract]
[WebInvoke(Method = "POST", UriTemplate = "/task", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)]
TaskLibrary.Task CreateTask(TaskLibrary.Task myTask);
[OperationContract]
[WebInvoke(Method = "GET", UriTemplate = "/task", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)]
IList<TaskLibrary.Task> GetTasks();
[OperationContract]
[WebInvoke(Method = "DELETE", UriTemplate = "/task", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)]
bool DeleteTask(string taskId);
[OperationContract]
[WebInvoke(Method = "PUT", UriTemplate = "/task", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)]
bool UpdateTask(TaskLibrary.Task myTask);
[OperationContract]
[WebInvoke(Method = "GET", UriTemplate = "/task/{taskId}", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)]
IList<TaskLibrary.Task> GetById(string taskId);
[OperationContract]
[WebInvoke(UriTemplate = "/task/completed", ResponseFormat = WebMessageFormat.Json, Method = "GET", RequestFormat = WebMessageFormat.Json)]
IList<TaskLibrary.Task> GetCompleted();
}
服务配置
<system.serviceModel>
<services>
<service behaviorConfiguration="TaskApi.ServiceBehavior" name="TaskService.TaskService">
<endpoint address="" binding="wsHttpBinding" contract="TaskService.ITaskContract">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="TaskApi.ServiceBehavior">
<!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
<serviceMetadata httpGetEnabled="true" />
<!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
参考指令
<%@ ServiceHost Language="C#" Debug="true" Service="TaskService.TaskService" %>
服务是从作为 WCF 服务库输出的程序集中挑选出来的
Url 重写以隐藏 svc 扩展
<system.webServer>
<validation validateIntegratedModeConfiguration="false" />
<rewrite>
<rules>
<rule name="Svc Extension remove pattern">
<match url="^([0-9a-zA-Z\-]+)/([0-9a-zA-Z\-\.\/\(\)]+)" />
<action type="Rewrite" url="{R:1}.svc/{R:2}" />
</rule>
</rules>
</rewrite>
</system.webServer>
- 我应该怎么做才能完成这项工作?
【问题讨论】:
-
你能显示你的服务配置吗?您的端点是如何定义的?你向我们展示了 .svc 文件服务指令吗?
-
@RichardBlewett 当然可以,等几分钟让我编辑帖子
-
@RichardBlewett 抱歉耽搁了,我已经添加了有关该问题的服务的配置信息。你能帮忙解决吗
标签: wcf url browser uritemplate webget