views:

3730

answers:

7

Is it possible to create a REST web service using ASP.NET 2.0? The articles and blog entries I am finding all seem to indicate that ASP.NET 3.5 with WCF is required to create REST web services with ASP.NET.

If it is possible to create REST web services in ASP.NET 2.0 can you provide an example.

Thanks!

A: 

I'm only just beginning to use them, but from what I've seen 2.0 pretty assumes SOAP.

Joel Coehoorn
+1  A: 

You can certainly create RESTful web services in ASP.NET 2.0, for example, but there are no high-level APIs to do all the donkey work for you, as provided by WCF in .NET 3.5.

Ubiguchi
+3  A: 

I have actually created a REST web service with asp.net 2.0. Its really no different than creating a web page.

When I did it, I really didn't have much time to research how to do it with an asmx file so I did it in a standard aspx file. I know thier is extra overhead by doing it this way but as a first revision it was fine.

protected void PageLoad(object sender, EventArgs e)
{
    using (XmlWriter xm = XmlWriter.Create(Response.OutputStream, GetXmlSettings()))
    {
        //do your stuff
        xm.Flush();
    }
}

    /// <summary>
    /// Create Xml Settings object to properly format the output of the xml doc.
    /// </summary>
    private static XmlWriterSettings GetXmlSettings()
    {
        XmlWriterSettings xmlSettings = new XmlWriterSettings();
        xmlSettings.Indent = true;
        xmlSettings.IndentChars = " ";
        return xmlSettings;
    }

That should be enough to get you started, I will try and post more later.

Also if you need basic authentication for your web service it can be done, but it needs to be done manually if you aren't using active directory.

Nathan Lee
+1  A: 

Well, of course you could always implement the spec yourself. It's just that there's nothing built-in to support it. If you use Nathan Lee's solution, do it as an http handler (.ashx) rather than an aspx. You can just about copy/paste his code into a new handler file.

Joel Coehoorn
+1  A: 

You can do RESTful web services easily by implementing the spec using IHTTPHandlers.

FlySwat
+1  A: 

It is definitely possible to create RESTful web services using ASP.NET. If you are starting a new project I would definitely look into creating RESTful web services using WCF. The 3.5 .NET Framework allows you to specify RESTful endpoint along with a regular old SOAP endpoint and still deliver the same service.

All you really have to do is enable an endpointbehavior that calls out <webHttp />

Here is a good series on creating RESTful web services using WCF:

http://blogs.msdn.com/bags/archive/2008/08/05/rest-in-wcf-blog-series-index.aspx

jdiaz
+1  A: 

Also check out using ASP.Net MVC. I've written some articles on this at my blog:

http://shouldersofgiants.co.uk/Blog/

Look for my Creating a RESTful Web Service Using ASP.Net MVC series

Piers