Windows Azure - Accessing the Request.Url Property!

So I've blogged previously about a known issue regarding port translation/mapping in Windows Azure, and today (well, today and a few weeks ago) I came across an interesting side effect of this issue when accessing the Request.Url property within a Windows Azure web role.

Now, anytime you access the HttpContext.Request.Url property (or any derivation of that), you will be unknowingly getting some extra info you may not want.

For example (and as it happens, the piece of code at the center of today's investigation), if you are building a new URL in your Windows Azure application by calling:

string RootUri = HttpContext.Current.Request.Url.GetComponents(UriComponents.SchemeAndServer, UriFormat.Unescaped);

You're going to get something along the lines of:

Scheme: Http

Server: x.cloudapp.net

Port: 20000 <-- Aha! (I added the <-- Aha! for dramatic effect)

This is going to cause your app issues if you are then trying to use that in a dynamic link or web request.

The solution is simple, anytime you call something that returns a Request.Url object, make sure you ensure any calls you make on that Url object do not explicitly return the port (this gets tricky when doing local testing where you need the port, so you'll need to manage that). Construct your new URL with only host or path details, otherwise you'll make mischief for yourself!

Enjoy :)

Technorati Tags: Windows Azure,Request.Url

Comments

  • Anonymous
    January 26, 2009
    excellent tip. love your blog. thanks.

  • Anonymous
    January 28, 2009
    FYI, I use the "Host" header on the request context (like HttpContext.Current.Request.Headers["Host"]). This has the added advantage that it works properly if your app is at a custom domain (like blog.smarx.com is).  It gives you exactly the host the user used to get to the page.

  • Anonymous
    March 08, 2009
    [Updated for MVC RC 2] Before you get started with ASP.Net MVC and Windows Azure – please install this

  • Anonymous
    April 20, 2009
    开头(有点离题了,原谅!)之前我就像某人说的那样,I’mnotquiteacloudguy,但是后来看了各式各样的演示,认识了CloudProject的构成,以及Mix09里面...

  • Anonymous
    April 20, 2009
    本文叙述了本人怎么将默认的 MVC 应用程序发布到云端,并给出了一些相关的文章和注意事项以及自己的想法。

  • Anonymous
    April 20, 2009
    本文叙述了本人怎么将默认的 MVC 应用程序发布到云端,并给出了一些相关的文章和注意事项以及自己的想法。