Jasinski Technical Wiki


Home Page
All Pages

Quick Search
Advanced Search »

Contributor Links

Create a new Page
File Management
Your Profile

Other Wiki Sections



Quick Reference - ASP.NET

Modified on Wed, Feb 12, 2014, 3:26 PM by Administrator Categorized as ASP·NET MVC, ASP·NET Web Forms, Quick Reference


Current HTTP Request objectHttpContext.Current.Request
Session IDstring sessionId = HttpContext.Current.Session.SessionID;
Maintaining scroll position on postbackIn the <%@ Page %> directive, set the MaintainScrollPositionOnPostback property to true
Disabling caching of a page.In the Page_Load event handler: Response.Cache.SetCacheability(HttpCacheability.NoCache)

Client Information

IP addressRequest.UserHostAddress
Computer Nameusing System.Net;

string remoteHost = Request.ServerVariables["REMOTE_HOST"];
IPHostEntry client = Dns.GetHostEntry(remoteHost);
string clientName = client.HostName;
uxClientNameLabel.Text = clientName;
User Namestring loginName = System.Threading.Thread.CurrentPrincipal.Identity.Name

using System.Net;
string loginName = Request.ServerVariables["REMOTE_USER"];

or string loginName = Request.ServerVariables["AUTH_USER"];
or string loginName = System.Web.HttpContext.Current.User.Identity

URL of Application Root

The following method will return, for example, http://localhost:2297/Version 1.0/. This is the root directory of the web application: the current page's URL, stripped of the path, filename, and any query string. For more information on using this function with CSS, JS, and XSL files, go here.

Public Shared ReadOnly Property ApplicationRootUrl() As String
        Dim r As HttpRequest = HttpContext.Current.Request
        Dim result As String = r.Url.Scheme   ' e.g., "http"
        result &= System.Uri.SchemeDelimiter  ' e.g., "://"
        result &= r.Url.Authority             ' e.g., "localhost:86065"
        result &= r.ApplicationPath           ' e.g., "/MyPath/"

        If Not result.EndsWith("/") Then
            result &= "/"
        End If

        Return result
    End Get
End Property

public static string AppRootUrl
        var r = HttpContext.Current.Request;
        var result = r.Url.Scheme;      // e.g., "http"
        result += Uri.SchemeDelimiter;  // e.g., "://"
        result += r.Url.Authority;      // e.g., "localhost:86065"
        result += r.ApplicationPath;    // e.g., "/MyPath/"

        if (!result.EndsWith("/"))
            result += "/";

        return result;

ScrewTurn Wiki version Some of the icons created by FamFamFam. Except where noted, all contents Copyright © 1999-2024, Patrick Jasinski.