c# Request.Url.Host vs Request.Url.Authority

asp.net (2)

According to MSDN Authority includes the port number while Host does not. Another aspect is that Authority will escape reserved characters if need be.

Without knowing your application it is hard to say whether it is a good idea, but in general I would suspect that it won't break anything... so go ahead...

Another option is to run the application IIS instead of IIS Express...

I've inherited an ASP.NET web application written in C#. In many pages throughout the site the hostname is retrieved using:

BaseHost = Request.Url.Host;

Since I am using Visual Studio 2012 Express and it's installed local IIS Express server, I seem to be stuck with a port number appended to the hostname (localhost) when I debug/run locally. The above code does not result in the port number being included and as such breaks links that are generated by code (menu item links, redirects, etc).

I see that I can overcome the issue by changing the code to:

BaseHost = Request.Url.Authority;

This seems to fix it by including the port when I'm running locally (localhost:4652) and when published to my staging server (development.mysite.com).

My question: Is this bad form? Is there a time or a situation in which this is going to cause problems on my live site? It just seems a lot easier to do a quick replace of all these instances. I've considered writing a small routine to append : with Request.Url.Port, but it seems easier just to use Request.Url.Authority. Too easy maybe...

I've tried to research my question online and at MSDN, but I don't see an answer.

My problem with this is that it ALWAYS adds the port, even when the port is not required. This can cause issues with multiple servers in some cases For example, in a production server environment behind a firewall on a pair of load-balanced web servers, it kept putting the firewall port in place, but that caused the URL to break because the port was tied to a specific web server in the server farm that wouldn't map correctly through the firewall. So I would be very careful with this method if you're using it across multiple servers. It caused a breaking issue with our application and had to be reverted back to using Url.Host. Plus, it made production web URL's look weird with the port number.