由于我是 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();
但http://localhost/api/tasksapi.svc/GetCompleted
浏览器中的 url 仍然以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>
- 我应该怎么做才能完成这项工作?