So doing some reading on ASP.NET Webservices and ASP.NET AJAX's ScriptManager Tag, I realized ASP.NET Webservices can handle SOAP or JSON if you set them up right. So below is what I have figured out. And my thoughts on the whole experience.
First off, you need to have ASP.NET AJAXs installed. Now make a refrence to System.Web.Extentions. Once that is done you can get to the meat of it.
Start off by making a quick change to your web.config so it can support passing .asmx requests to a script handler.
using System;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Web.Script.Services;
///
/// Summary description for WebService
///
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ScriptService]
public class WebService : System.Web.Services.WebService {
public WebService () {
//Uncomment the following line if using designed components
//InitializeComponent();
}
[WebMethod]
public string HelloWorld(string echoField) {
return echoField;
}
}
Now its time to play with your HTML. Now I am on this kick of trying to write 100% HTML clients, so I won't be using the scriptmanager tag (that would be too easy).. But if I were to use the scriptmanager tag then all I would need to do is write one line of javascript to call the above webservice. As such my script would look something like this (so that it can handle processing the request)
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
<script type="text/javascript">
function testCall()
{
debugger;
WebService.HelloWorld("test message",completeTest,errorTest);
}
function completeTest(val)
{
alert(val);
}
function errorTest(error)
{
alert(error.get_message());
}
</script>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
<Services>
<asp:ServiceReference Path="Services/WebService.asmx" />
</Services>
</asp:ScriptManager>
<div>
<button onclick="testCall()">test</button>
</div>
</form>
</body>
</html>
But like I said before I wasn't going to USE microsofts framework on the client. if I did the page would be ASP.NET not 100% Html :-P.. So this is what I did. first off I have to choose a AJAXs API, in this case I used Prototype.js but I like YUI and could have used it :-).. Once I have added my API to my page I would call my JSON webservice and it too is only one line (although not as pretty as Microsofts provides. As such it looks something like this:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>Untitled Page</title>
<script type="text/javascript" src="scripts/prototype.js"></script>
<script type="text/javascript">
function testCall()
{
new Ajax.Request('Services/WebService.asmx/HelloWorld',
{ postBody:"{echoField:'test message'}",
method: 'post', contentType:'application/json; charset=utf-8',
onSuccess: completeTest,
onFailure: errorTest});
}
function completeTest(val)
{
alert(val.responseText);
}
function errorTest(error)
{
var val = error.responseText;
alert(val);
}
</script>
</head>
<body>
<div>
<button onclick="testCall()">test</button>
</div>
</body>
</html>
Although I could use the ASP.NET AJAX provided javascript API and it works great, I'm having more fun making it work with prototype. Plus this is a proof of concenpt, which means you can find the smallest lightest JSON communication layer you want and use that (jquery is only 50k uncompressed and should work fine).. I think next I'll have to come up with an easy way to wrap these webservices in a proxy much like microsoft does OR find a way to use the microsoft generated Proxies with other APIs.. By the way to see the Microsoft generated Javascript proxy, just put /js at the end of your webservice URL. (scriptmanager does this for you normally).
Anyways, this was a fun little experiement, and I'll post when I get it more refined.