Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Possible to create REST web service with ASP.NET 2.0

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!

like image 230
markus Avatar asked Sep 02 '08 21:09

markus


People also ask

Is ASP Net Web API RESTful?

ASP.NET Web API is an ideal platform for building RESTful services.

What is RESTful services in asp net?

What is REST or RESTful service? REST stands for Representational State Transfer. It is an architectural concept for building inter-operable light weight web services which helps to access and manipulate the web resources identified though URI (Uniform Resource Identifier).

What is RESTful API in C#?

In simple terms a REST API allows applications to interact with each other and exchange data. For example, let's say you are building a mobile application or a web application. In that application you want to display weather data like temperature, humidity, wind speed etc.


6 Answers

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.

like image 156
Nathan Lee Avatar answered Oct 01 '22 12:10

Nathan Lee


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

like image 28
jdiaz Avatar answered Oct 01 '22 11:10

jdiaz


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.

like image 31
Ubiguchi Avatar answered Oct 01 '22 10:10

Ubiguchi


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.

like image 45
Joel Coehoorn Avatar answered Oct 01 '22 10:10

Joel Coehoorn


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

like image 37
FlySwat Avatar answered Oct 01 '22 11:10

FlySwat


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

like image 33
Piers Avatar answered Oct 01 '22 12:10

Piers