这里是我的简单的例子:对WCF托管REST Web服务与IIS
的web.config:
<?xml version="1.0"?>
<configuration>
<system.serviceModel>
<services>
<service name="Services.DBService">
<endpoint address="http://localhost/" binding="webHttpBinding"
contract="Contracts.IDBService" />
</service>
</services>
<behaviors>
<endpointBehaviors>
<behavior name="">
<webHttp defaultOutgoingResponseFormat="Json" />
</behavior>
</endpointBehaviors>
</behaviors>
</system.serviceModel>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
</startup>
</configuration>
支持下列合同:
using System.ServiceModel;
namespace Contracts
{
[ServiceContract]
public interface IDBService
{
[OperationContract]
Services.Person GetData(string id);
}
}
和服务:
using System.ServiceModel.Web;
namespace Services
{
public class DBService : Contracts.IDBService
{
[WebInvoke(Method = "GET", UriTemplate = "data/{id}")]
public Person GetData(string id)
{
return new Person()
{
Id = int.Parse(id),
Name = "John Doe"
};
}
}
public class Person
{
public int Id { get; set; }
public string Name { get; set; }
}
}
跑步通过调试器中的IIS Express,我可以向localhost/data/10发出GET请求。
但是,当我尝试将本地发布到IIS时,我尝试访问本地主机/数据/ 10时出现404错误。我把它放在默认的web应用程序中,并且有一个localhost的根。
有什么我需要知道的使用我不知道的UriTemplate
?我做错了什么?
它是运行.NET 4.0的应用程序池的一部分,并且此应用程序池已启动。错误代码:
Module IIS Web Core Notification MapRequestHandler Handler StaticFile Error Code 0x80070002 Requested URL http://localhost:80/Data/10 Physical Path C:\inetpub\wwwroot\Data\10 Logon Method Anonymous Logon User Anonymous
它试图找到物理位置,显然它不存在。我怎样才能配置它不这样做?
不知道给什么其他的信息,但要求您将收到...
这不是可怕很有帮助,但对于REST,WCF已关闭,并且MVC/WebAPI位于。 – Chet
WebInvoke属性应该与POST请求一起使用。对于GET,您应该使用WebGet属性。 – Mimas