Rest – Possible to create REST web service with ASP.NET 2.0

restweb services

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!

Best Answer

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.

Related Topic