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


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: ,

Comments (6)

  1. excellent tip. love your blog. thanks.

  2. 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 is).  It gives you exactly the host the user used to get to the page.

  3. [Updated for MVC RC 2] Before you get started with ASP.Net MVC and Windows Azure – please install this

  4. DiryBoy says:


  5. DiryBoy says:

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

  6. DiryBoy says:

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

Skip to main content