Quantcast how to get current domain name in asp.net

how to get current domain name in asp.net

by Neon Quach 23. February 2011 23:20

In this blog I share my snippets code which can get your current domain name using C# in asp.net

We have three ways to complete this task

using System;
using System.Collections.Specialized;
using System.Web;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        // 1st
        Response.Write(Request.Url.Scheme + Uri.SchemeDelimiter + Request.Url.Host + (Request.Url.Port != 80 ? ":" + Request.Url.Port : ""));
        Response.Write("</br>");
        NameValueCollection variables = HttpContext.Current.Request.ServerVariables;
        string protocol = (variables["SERVER_PORT_SECURE"] == "1") ? "https://" : "http://"string domain = variables["SERVER_NAME"];
        string port = variables["SERVER_PORT"];
        // 2nd
        Response.Write(protocol + domain + port);
        Response.Write("</br>");
        // 3rd
        string url = HttpContext.Current.Request.Url.AbsoluteUri.Replace(HttpContext.Current.Request.Url.AbsolutePath, "");
        Response.Write(url);
    }
}


Hope this help!

Tags: ,


Categories: asp.net | c#

blog comments powered by Disqus

About me

I'm  currently employed as Software developer at devinition.com and also a Microsoft Certified Technology Specialist (MCTS), Microsoft Certified Professional Developer (MCPD) in Net Framework 2.0 and 3.5: Web Applications and MCTS .NET Framework 3.5, ADO.NET Applications

Powered by BlogEngine.NET 2.5.0.5 - Eco Theme by n3o Web Designers