The World’s Leading Microsoft .NET Magazine
   
 
The .NET Addict's Blog

My Top Tags

                                                           

My RSS Feeds








I heart FeedBurner

Latest Diggs - Programming

Computers Blogs - Blog Top Sites

Site Hits

Total: 4,896,067
since: 19 Jan 2005

Creating a RESTful POX Service using the ASP.NET MVC Framework

posted Wed 09 Jul 08

A couple of blog posts ago, I gave an example of how to consume a POX service using Cocoa because I had very little luck using WSDL/SOAP-based services. Since then, I've seen some examples of how to consume SOAP from Cocoa clients, but the code looks hideous and every time I attempt to do anything with SOAP, everything seems to become a snowball of endless complexity.

Anyhow.. The first thing we need for our service is a controller. So, here's the SampleDataController that drove the example from the previous blog post:

namespace SampleBackEnd.Controllers
{
    public class SampleDataController : Controller
    {
        public ActionResult Index()
        {
            List<string> foo = new List<string>();
            foo.Add("Rachael Ray");
            foo.Add("Mario Vitale");
            foo.Add("Bobby Flay");
            foo.Add("Alton Brown");

            this.ViewData["strings"] = foo.ToArray();
            return View();
        }
    }
}

This is a really, really simple example. Basically the job of the controller is to perform the action being requested. In this case, it's the Index() method , which is the default controller method when none is specified.This controller loads up the data (in this case, a sampling of Food Network chefs) and then makes sure that the view (remember, this IS MVC after all...) has all of the data it needs in order to render.

So, how do we make a POX service view instead of an HTML view? That's one of the reasons why I'm dumping WCF for my POX services and using ASP.NET MVC - it's so unbelievably easy, it should be illegal.

First, in the code-behind for the view, you need to change the content type from HTML to XML:

namespace SampleBackEnd.Views.SampleData
{
    public partial class Index : ViewPage
    {
        protected override void CreateChildControls()
        {
            base.CreateChildControls();

            Response.ContentType = "text/xml";
        }
    }
}

Secondly, you just modify your view so that instead of rendering HTML, it's rendering XML:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Index.aspx.cs" 
  Inherits="SampleBackEnd.Views.SampleData.Index" %>
<SampleData>
    <%  string[] stringList = (string[])ViewData["strings"];
        foreach (string s in stringList)
       { %>
        <FoodNetworkStar name="<%= s %>" randomData="Random Data" />
    <% } %>
</SampleData>

Note how the inline use of C# here actually makes things easier and more readable, unlike the old days of ASP/VBScript where its inline nature made everything suck hard.

That's it. You're done. You now have a fully functioning RESTful POX service. If you need windows authentication wrapped around that, its a no-brainer because you're using ASP.NET. Same thing if you need cookie-based authentication for your service. If you want token-based (my own personal preference), then you just create a TokenController that is responsible for maintaining active logins, and make your other controllers require tokens.

tags:                  

links: digg this    del.icio.us    technorati    reddit

AddThis Social Bookmark Button




1. hhrvoje left...
Thu 10 Jul 08 1:07 am :: http://twitter.com/hhrvoje

well, you don't have to have view, you can just spit xml from controller, something like this:

var xml= new XElement("SampleData",

  • from el in foo

  • select new XElement("FoodNetworkStar",

    • new XAttribute("name",el),

    • new XAttribute("radnomDaza","Random Data),

    • ));

Response.ContentType = "text/xml"; Response.Write(xml.ToString());


2. Kevin Hoffman left...
Thu 10 Jul 08 5:03 am

If you are rendering XML or HTML from the controller then you are doing an end-run around MVC and might as well just use a WCF service. The beauty of MVC is that your view and controller are clearly separated and unit testable independent from each other.


3. Piers left...
Thu 16 Oct 08 6:27 pm :: http://shouldersofgiants.co.uk/blog/

I've started writing a series of blog posts on this very topic:

  • http://shouldersofgiants.co.uk/blog/

One of the things I cover is spitting out different representations, my web service can create XHTML, XML and JSON. I have a fourth representation called "help" that lets you test the whole thing from your browser.


Tag Related Posts

Html.JqGrid - Cleaning up your jqGrid Code

Tue 22 Dec 09 8:46 P GMT-05
tags:              

How to Build your First Azure-Powered MVC App

Tue 29 Sep 09 2:16 P GMT-05
tags:        

ViewState is the Froo-It of the Dev-Il

Wed 23 Sep 09 3:09 P GMT-05
tags:      

Fix for Minor Bug in ASP.NET MVC New Project Template

Mon 04 May 09 2:48 A GMT-05
tags:      

Building RESTful Java Web Services with JAX-RS

Mon 23 Feb 09 8:42 P GMT-05
tags:                    

Know thine Enemy: RESTful Web Services in ... Java!

Sun 11 Jan 09 2:08 P GMT-05
tags:                    

Enterprise Web Services Manifesto - Wire Formats

Tue 02 Sep 08 1:43 P GMT-05
tags:                

MobileMe vs. Live Mesh - Round 1

Wed 11 Jun 08 12:20 A GMT-05

NYC SharePoint Developer Needed

Mon 12 May 08 12:09 P GMT-05

Scott Guthrie Updates the ASP.NET MVC Roadmap

Wed 13 Feb 08 3:49 P GMT-05
tags:    

Volta is to Ajax what Tums is to my Stomach

Wed 30 Jan 08 4:11 P GMT-05

LINQ to REST - A much better name for Astoria

Tue 11 Dec 07 1:23 P GMT-05
tags:        

ASP.NET 3.5 Extensions Preview Released

Mon 10 Dec 07 2:10 P GMT-05

Microsoft unveils an MVC framework for ASP.NET

Mon 08 Oct 07 12:58 P GMT-05
tags:      

Exploring the MVC Pattern in WPF

Tue 10 Apr 07 12:51 P GMT-05
tags:                      

What I think is a bug in WCF POX messaging

Thu 04 Jan 07 4:58 P GMT-05
tags:      

ASP.NET Ajax v1.0 Beta

Fri 27 Oct 06 6:17 P GMT-05
tags:      

ASP.NET vs Ruby on Rails : Round 2 (Agility)

Thu 05 Oct 06 11:02 A GMT-05
tags:                      

ASP.NET vs Ruby on Rails : Round 1

Wed 04 Oct 06 1:37 P GMT-05
tags:                

SOA Manifesto

Thu 26 Jan 06 10:38 P GMT-05
tags: