|
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.
well, you don't have to have view, you can just spit xml from controller,
something like this:
from el in foo
select new XElement("FoodNetworkStar",
new XAttribute("name",el),
new XAttribute("radnomDaza","Random Data),
));
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.
I've started writing a series of blog posts on this very topic:
http://shouldersofgiants.co.uk/blog/