Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Request.Url.Host vs Request.Url.Authority

Tags:

c#

asp.net

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.

like image 525
rwkiii Avatar asked Aug 18 '13 20:08

rwkiii


People also ask

What is a URL authority?

The Authority property is typically a server DNS host name or IP address. This property might include the service port number if it differs from the default port for the URI. If the Authority component contains reserved characters, these are escaped in the string value returned by this property.

What is authority host?

Host Authority means the local authority appointed by the Parties under these arrangements to lead on a specified matter or function as set out in paragraphs 14 and 19.

What is context request host?

Context. Request. Host is the value of the HTTP Host header.


2 Answers

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...

like image 172
Yahia Avatar answered Oct 17 '22 17:10

Yahia


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.

like image 20
HBlackorby Avatar answered Oct 17 '22 16:10

HBlackorby