Click here to Skip to main content
15,897,518 members
Articles / Web Development / IIS

Build ReST based Web Services in .NET/C#

Rate me:
Please Sign up or sign in to vote.
5.00/5 (12 votes)
12 Jul 2009CPOL2 min read 123.2K   5.5K   55  
A ReST based Web Service for C#.
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="tstWebApplication1._Default" %>

<!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 runat="server">
    <title>Untitled Page</title>
    <script language="javascript">
        function getHTTPObject() {
            if (typeof XMLHttpRequest != 'undefined') {
                return new XMLHttpRequest();
            }
            try {
                return new ActiveXObject("Msxml2.XMLHTTP");
            } catch (e) {
                try {
                    return new ActiveXObject("Microsoft.XMLHTTP");
                } catch (e) {}
            }
            return false;
        }
        function doExchange(url, method, dBody){
            var http = getHTTPObject();
            http.open(method, url, true);
            http.onreadystatechange = function() {
	            if (http.readyState == 4) {
		            doSomethingWith(http.responseText);
	            }
            }
            alert(dBody);
            http.send(dBody);//
        }
        function doSomethingWith(response){
            document.getElementById('responseMessageHere').innerText = response;
        }
        var aPartBody = '<Part><Id>1103</Id><Name>Tablet Special</Name><Description>XP Tablet Special</Description></Part>';
        function getNewPartBody(){
            var partname = document.getElementById('PartName').value;
            if(partname.replace(/\s/gi,'').length == 0) {alert('No Part Name'); return;}
            if(partname.replace(/\w/gi,'').length > 0){alert('Special Chars not allowed'); return;}
            var partDescript = document.getElementById('PartDescript').value;
            if(partDescript.replace(/\w/gi,'').length > 0){alert('Special Chars not allowed'); return;}
            return '<Part><Id></Id><Name>'+partname+'</Name><Description>'+partDescript+'</Description></Part>';
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <input type="button" value="List" onclick="doExchange('http://localhost:1055/RESTService/Parts/','GET',null)" />
    <input type="button" value="Update" onclick="doExchange('http://localhost:1055/RESTService/Parts/','Put',aPartBody)" />
    
    <div style="border:1px solid black">
    <div>Create New</div>
        <input id="PartName" type="text" value="Part Name" />
        <input id="PartDescript" type="text" value="Part Descript" />
        <input type="button" value="Create" onclick="var newPart = getNewPartBody();if(newPart)doExchange('http://localhost:1055/RESTService/Parts/','Post',newPart)" />  
    </div>
    <div id="responseMessageHere"></div>
    </div>
    </form>
    
</body>
</html>

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
Loves coding...

Comments and Discussions