Click here to Skip to main content
12,694,597 members (32,741 online)
Click here to Skip to main content

Stats

85.6K views
5.2K downloads
57 bookmarked
Posted

Build ReST based Web Services in .NET/C#

, 12 Jul 2009 CPOL
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)

Share

About the Author

Parag.Gadkari
United States United States
Loves coding...

You may also be interested in...

Pro
Pro
| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170118.1 | Last Updated 12 Jul 2009
Article Copyright 2009 by Parag.Gadkari
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid