Archive for March, 2012

Making Cross-Domain Calls in SignalR

March 30, 2012

I saw this stack overflow post today on how to use SignalR across domains:

Since I wanted to try out SignalR anyway I thought it would be a nice way to get started and see if I can solve the problem and write a small tutorial on how to do it.

The first step in using SignalR cross-domain is to tell it where to find the hub to connect to. This is done using the following code that you should insert at the beginning of your JavaScript code:

$.connection.hub.url = 'http://[someotherdomain]/signalr';

If you try your application like this it will not work and you will have the same problem as the poster mentioned above. This is because for security reasons many browsers don’t allow cross-domain calls by default. A server has to add the special Access-Control-Allow-Origin header to its response to tell the browser to allow cross domain calls to this specific site.

You can add this value to your ASP.NET page using:

Response.AppendHeader("Access-Control-Allow-Origin", "*");

However even with this modification SignalR won’t do the cross domain call – at least not in Internet Explorer 9 where I tested it. Like the original poster I was stuck for quite a while because I didn’t find any solutions. The error message I got was:

SignalR: Connection must be started before data can be sent. Call .start() before .send() 

Poking arround the jQuery code and debugging a bit I eventually saw that there is a check inside jQuery to see if the call is a cross domain call or not and eventually this part of the code will terminate with the error “No transport”.

With this new error message I came upon this page explaining the problem and suggesting a possible solution to force jQuery into making a cross domain call using the following JavaScript line: = true; //force cross-site scripting

And it worked!!!

I also found some references suggesting JSONP as a possible solution, but it seems more complex and at least for me the solution above works fine.

Creating an URL to a WebAPI ApiController inside an ASP.NET MVC View

March 27, 2012


Today I was trying to add some dynamic content to my new IT consulting website. My website is based on ASP.NET MVC, but I needed to load some dynamic content in one part of the page and I thought I would try out the new ASP.NET WebAPI framework for this.


Creating the ApiController was easy, but when I wanted to use the controller in my ASP.NET MVC view, I wasn’t sure how to include the Url of the controller. Hardcoding the address didn’t feel right, especially because for the MVC controllers you have the really useful Url.Action method, which takes the name of an action and a controller and generates an URL to call this action. Unfortunately there wasn’t any built in method to call an ApiController. My first attempt was to use the following call instead, specifying the name of the route I want to use directly:

Url.RouteUrl("DefaultApi", new{controller="[CONTROLLERNAME]"})

Unfortunately, this didn’t work as expected — the function didn’t produce any Url.

After some googling I came across this stackoverflow post, with the missing piece of the puzzle:


Basically all you have to do is to add an empty httproute parameter to the Url.RouteUrl call. This apparently helps the system differentiate between normal routes and WebApi routes. So the final call looks like this:

Url.RouteUrl("DefaultApi", new{httproute = "", controller="[CONTROLLERNAME]"})

Keep in mind that the first parameter to RouteUrl refers to the name of the route you used in the RegisterRoutes function, which is “DefaultApi” by default.

Akzente.IT – New web site online!

March 22, 2012

My new web site at akzente.IT is online!

You will find some more information about me and my projects there.

My goals for the web page were twofold. First of all I wanted to have a nice virtual “business card” – a place where I can present myself to potential employers with a list of my skills and the projects I have done in the past.

My second goal was to use this opportunity to learn more about ASP .NET MVC (with Razor) and HTML5 and I have to say I’m very happy with the results. I think it was the first time I actually enjoyed creating a website. I have full control of the generated HTML, I didn’t have any hard to debug errors so far and the separation that MVC gives me is really cool. I’m actually using this to generate different views of my project page. I have a single controller responsible for the list of projects which delegates to different views depending on an input parameter (e.g. to generate a text only version for carreer networks) and it works quite nice. I’m thinking about extending this to automatically create a resumé in Word generating Office Open XML with a Razor view. I’m curious if this works.

Another interesting page is the blog page where I show some recent blog posts with links back to this blog. It was amazing how easy it was to build this.

public class BlogController : Controller
    [OutputCache(Duration = 60*60, VaryByParam = "none")]
    public ActionResult Index()
        var model = new Blog();
        string feedUrl = "";
        using (XmlReader reader = XmlReader.Create(feedUrl))
            var feed = SyndicationFeed.Load(reader);
            model.Feed = feed;
        return View(model);

My favourite is the controller attribute for output caching, which will cache the generated result for 1 hour, so that it loads faster and doesn’t hit the blog to hard in case there are many visitors.

That’s all about the website for now. I also have a profile now on GULP, check it out: