I’m supposed to reach a resource using JS from our server. I’ve set up the code below.
var targetUrl = "http://somePlace.com/actualResourceName";
var xdr = new XDomainRequest();
xdr.onload = function () { alert(xdr.responseText); }
xdr.open("GET", targetUrl);
xdr.send();
However, I’m not clear how the method on the other side needs to be created. I’ve come up with the following suggestion, fully aware that it’s not working. I’m sure I’m missing the right attributes, for instance. I’m not even sure where to set that the method is supposed to react on actualResourceName…
[???]
public String ActualResourceName()
{
return "Bye, bye, cruel word!";
}
I’ve googled around but I haven’t found any solution. I might have stumbled across it without realizing that it’s something useful, though.
How should the method in C# be written?
OK, here it goes. I’ll describe two ways to get it done, easiest ones:
1. ASP.NET WebApi
You’ll have to create a new ASP.NET MVC4 project (be it release or RC), select the ‘WebApi’ as an option:

And you’ll have the template ready. Now you right click ‘Controllers’ folder, then

Add -> Controller:
and fill it out e.g. like that:
Default routes are in Global.asax, when you go to definition of
WebApiConfig.Register(...)method, you’ll see that the default route ishost/api/controller.Let’s try that out, when you start up the project and go under (in my case, the port is chosen automatically by the development server)
http://localhost:23030/api/ActualResourceyou’ll get:
WebApi returns either JSON or XML depending on the
Acceptheader, if you want JSON to be the only one/default, take a look at this link.You can of course create a class and return it, it will get serialized to XML/JSON in a similar fashion you’ll see below with ServiceStack.
2. ServiceStack
Now the ServiceStack is powerful, open-source REST web-service framework. It works a little bit different than WebApi, and here’s a quick introduction (although the documentation is good):
Create the regular ASP.NET MVC project (in my case, MVC4) – you’ll have an empty template:
Then fire up the Package Manager Console and type (like the documentation suggests)
Install-Package ServiceStack.Host.Mvc, which will get you a ServiceStack project template with a tutorial app and such which you can later on remove if you wish.But first things first, the ServiceStack works on DTOs, the Request-Response objects. So let’s create them, the
ActualResourceclass which will serve as a request andActualResourceResponsewhich will be a response. Since you have no parameters in a request, the first one is trivial:Any parameters would be automatic properties. Now the response:
And the service class itself:
You could of course return bare
stringfor your current purposes, it would work all the same.Now in the template ServiceStack creates, everything happens in AppHost.cs file, let’s take a look and modify it a tiny little bit:
For it to work, you have to go to Global.asax and comment out the whole
WebApiConfig.Register(GlobalConfiguration.Configuration)line, then go into theRouteConfig.RegisterRoutesmethod and add:A little more plumbing is required, but it’s still not bad.
Now when you start the service, go under

localhost:whateverport/api/actualResource, you’ll get the familiar string, and here’s a screenshot:ServiceStack can serialize to various formats, so if you go under
http://localhost:yourPort/api/actualResource?format=json, you’ll get:if
?format=xml, then:And so on…
Now the ServiceStack setup is a bit more complicated, but it supports e.g. Memcache out of the box, you can plumb in Redis, you can use various authentication providers, all of that may be quite useful in some scenarios. But, as Uncle Ben once said, “with great power comes great responsibility”, and a bit harder setup phase…
Now you can choose whichever you feel like, these two are the simplest options right now IMHO. Of course it’s only a simple tutorial to get you started, you’ll have a chance to explore this topic in depth when you get going with the project.