Jasinski Technical Wiki

Navigation

Home Page
Index
All Pages

Quick Search
»
Advanced Search »

Contributor Links

Create a new Page
Administration
File Management
Login/Logout
Your Profile

Other Wiki Sections

Software

PoweredBy

Quick Reference - ASP.NET

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

General

ItemCode
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

ItemCode
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
    Get
        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
{
    get
    {
        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 3.0.1.400. Some of the icons created by FamFamFam. Except where noted, all contents Copyright © 1999-2024, Patrick Jasinski.