How do I get just the domain part of the URL asp.net c#

JustAskMe
JustAskMe used Ask the Experts™
on
I want to use the domain part (http://www.mysite.com) as a string for a sql parameter.

How do I get just the domain part of the page that runs my C# code?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
Request.ServerVariables("server_name")

Or C# would probably be Request.ServerVariables["server_name"]

Author

Commented:
Is there a better way for asp.net to do that?

Commented:
Well, you could hard-code the value in your application. But you have to change it whenever the application moves to another domain.

Seriously, I don't why you need "better".
Introduction to Web Design

Develop a strong foundation and understanding of web design by learning HTML, CSS, and additional tools to help you develop your own website.

Author

Commented:
No I don't want it hard coded, My problem with this code is that it doesn't look like vs is accepting it

Commented:
Response.write(Request.Servervariables["server_name"]);

works fine in my VS.

Commented:
Hi,

You could use the Uri class

      System.Uri uri = new Uri("http://www.mysite.com/default.aspx");
      Console.WriteLine(uri.Host);


/peter
try out this

string domain = "http://" + HttpContext.Current.Request.ServerVariables["HTTP_HOST"];

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial