I finally got totally PO and went off to business full contact. This is what I've produced - it works on my machine and I hope it's not a local phenomenon. :)
IRestService.cs - the declaration, what your code promises to a contacting client
[ServiceContract]
public interface IRestService
{
[OperationContract]
[WebInvoke(Method = "GET",
ResponseFormat = WebMessageFormat.Xml,
BodyStyle = WebMessageBodyStyle.Wrapped,
UriTemplate = "xml/{id}")]
String XmlData(String id);
[OperationContract]
[WebInvoke(Method = "GET",
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Wrapped,
UriTemplate = "json/{id}")]
String JsonData(String id);
}
RestService.svc.cs - the implementation, what your code actually does to the client
public class RestService : IRestService
{
public String XmlData(String id)
{
return "Requested XML of id " + id;
}
public String JsonData(String id)
{
return "Requested JSON of id " + id;
}
}
Web.config - the configuration, what your code is handled as on the way to the client
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.serviceModel>
<services>
...
</services>
<behaviors>
</behaviors>
</system.serviceModel>
</configuration>
services - contents of the tag describing the service's nature
<service name="DemoRest.RestService"
behaviorConfiguration="ServiceBehavior">
<endpoint address="" binding="webHttpBinding"
contract="DemoRest.IRestService"
behaviorConfiguration="web"></endpoint>
</service>
behaviors - contents of the tag describing the behavior of the service and the end-point
<serviceBehaviors>
<behavior name="ServiceBehavior">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="web">
<webHttp/>
</behavior>
</endpointBehaviors>
Index.html - the executor, what your code can be called as
<html>
<head>
<script>
...
</script>
<style>
...
</style>
</head>
<body>
...
</body>
</html>
script - contents of the tag describing the executable in JavaScript
window.onload = function () {
document.getElementById("xhr").onclick = function () {
var xhr = new XMLHttpRequest();
xhr.onload = function () { alert(xhr.responseText); }
xhr.open("GET", "RestService.svc/xml/Viltersten");
xhr.send();
}
}
style - contents of the tag describing the appearance
.clickable
{
text-decoration: underline;
color: #0000ff;
}
body - contents of the tag describing the markup structure
<ul>
<li>XML output <a href="RestService.svc/xml/123">
<span class="clickable">here</span></a></li>
<li>JSON output <a href="RestService.svc/json/123">
<span class="clickable">here</span></a></li>
<li>XHR output <span id="xhr" class="clickable">here</span></li>
Everything is stored in a project called DemoRest
. I created my own files for declaration and implementation of the service, removing the default ones. The directives of using
as well as the XML version declaration are omitted for spacial reasons.
Now the response can be retrieved using the following URL.
localhost:12345/RestService.svc/xml/Konrad
localhost:12345/RestService.svc/json/Viltersten
- Does anybody else get it to work too?
- Any suggestions on improvement or clarification?