我正在搞清楚WCF / REST的东西。我的目标是创建端点没有扩展名的服务,所以我创建了一个简单的WCF服务:
namespace SampleWebSite
{
[ServiceContract()]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class ContactService
{
[WebGet(UriTemplate = "all", ResponseFormat = WebMessageFormat.Json)]
[OperationContract]
public string[] Sample()
{
return new string[] { "Bill Gates", "Paul Allen", "Steve Ballmer" };
}
}
}
所以,我希望它的功能可以通过有意义的网址获得,比如http://localhost/samplewebsite/ctsvc/all。我可以通过将以下代码放入Global.asax文件来轻松实现:
void Application_Start(object sender, EventArgs e)
{
RegisterRoutes();
}
void RegisterRoutes()
{
RouteTable.Routes.Add(new ServiceRoute("ctsvc", new WebServiceHostFactory(), typeof(SampleWebSite.ContactService)));
}
现在我试图找出是否可以摆脱这些代码并将所有魔法移到web.config文件中。我已经坚持了几天没有任何结果。我目前的配置如下:
<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
<services>
<service name="SampleWebSite.ContactService" behaviorConfiguration="Meta">
<endpoint name="rest"
address="ctsvc"
binding="webHttpBinding"
contract="SampleWebSite.ContactService"
behaviorConfiguration="REST" />
</service>
</services>
<behaviors>
<endpointBehaviors>
<behavior name="REST">
<webHttp/>
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="Meta">
<serviceMetadata httpGetEnabled="true" />
</behavior>
</serviceBehaviors>
</behaviors>
<standardEndpoints>
<webHttpEndpoint>
<standardEndpoint name="ctsvc" helpEnabled="true" automaticFormatSelectionEnabled="false"/>
</webHttpEndpoint>
</standardEndpoints>
</system.serviceModel>
从Global.asax中删除代码并使用此配置会导致我不断收到404错误。我做错了吗?