Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically get IIS server URL

Short version:

How can I get the URL of the server my MVC3 project is being executed on? I do NOT want to hard code a server address.

Long version:

I've got three servers I will be working with:

  1. Development server
  2. QA server for others to test
  3. Production server

None of the above servers have the same URL.

My web project needs to generate notification emails that contain a clickable link.

I know that Request.Url.ToString() will get me the server but if possible, I'd rather not spend time processing that string everytime I generate an email.

like image 834
John Stone Avatar asked Sep 23 '26 07:09

John Stone


1 Answers

You can process the Request.Url once and store the result in a static variable. Just make sure you access that variable in a thread-safe manner. I've been using this in production for quite a while, and it works like a charm.

like image 120
mnemosyn Avatar answered Sep 26 '26 02:09

mnemosyn



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!