TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

ASP.NET ServerVariables Example

This C# example code uses the ServerVariables collection in ASP.NET.

ServerVariables returns important information from Request.

It can get your ASP.NET website's domain name programmatically. Often ASP.NET code is used on many different domain names.

And: With ServerVariables, we don't need to hard code the domain name. This makes programs easier to maintain.

Example. You can quickly get the current website's domain name using the ServerVariables collection on the Request object. The example that follows uses the HttpContext.Current property, which means you can easily use it from a C# class.

Method that uses ServerVariables: C#

using System.Web;

/// <summary>
/// Contains utility methods for domain names.
/// </summary>
public static class DomainMethods
{
    /// <summary>
    /// Get the host with http as a string.
    /// </summary>
    public static string GetHost()
    {
	return "http://" + HttpContext.Current.Request.ServerVariables["HTTP_HOST"];
    }
}

This is a static class, which means it can only contain static methods and fields. It cannot be instantiated with a constructor. The class has one method defined, GetHost, which returns a string containing the host name.

Static Class

GetHost returns the host name beginning with "http://", in a single string. The HttpContext.Current.Request code accesses the current request and then looks up the value at the "HTTP_HOST" key in the ServerVariables collection.

Example 2. Next, we use the method. The code below prints the host name to the browser. Please note that localhost is the domain name when you are running locally in the development environment.

Webpage file that prints method result

using System;
using System.Web.UI;

public partial class _Default : Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
	base.Response.Write(DomainMethods.GetHost());
    }
}

This example is just a simple web forms page that calls the static method we defined earlier. It returns the string containing "http://" and the host name. This code works in the real world on websites.

And: I have this exact code running on a deployed website. It shows the actual domain, such as "http://example.com".

Summary. ServerVariables can be useful. We obtained the domain name and host name in an ASP.NET website with ServerVariables. You do not need to store the domain name in a configuration file such as Web.config.

Tip: I recommend using this code and caching the result in a static field for best performance.


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf