Getting current page URL (or it’s parts) in C#

Here is an easy reference for how to get different parts of an URL in C#.

I’ve color-coded the example URL below for easier matching with the values of the properties of the HttpRequest.

URL used for this example:

Value of HttpContext.Current.Request.Url.Host

Value of HttpContext.Current.Request.Url.Authority

Value of HttpContext.Current.Request.Url.AbsolutePath

Value of HttpContext.Current.Request.ApplicationPath

Value of HttpContext.Current.Request.Url.AbsoluteUri

Value of HttpContext.Current.Request.RawUrl

Value of HttpContext.Current.Request.Url.PathAndQuery

P/Invoke and LoadLibrary / GetProcAddress

A little snippet I wrote while trying to use LoadLibrary/GetProcAddress to call functions and get variable values that are exported by an unmanaged dll with P/Invoke.

extern "C" __declspec(dllexport) const char* _Version = "0.1";

extern "C" __declspec(dllexport) const char* GetMyVersion(void)
	return _Version;
public void TestPinvoke()
		IntPtr lib = LoadLibrary(@"C:\Documents and Settings\Administrator\My Documents\Visual Studio 2010\Projects\TestWinDll\Debug\TestWinDll.dll");
		if(lib == IntPtr.Zero)
				throw new Win32Exception(Marshal.GetLastWin32Error());

		IntPtr procAddress = GetProcAddress(lib, "GetMyVersion");
		var gmv = (GetMyVersion)Marshal.GetDelegateForFunctionPointer(procAddress, typeof(GetMyVersion));
		var ver = gmv();

		procAddress = Marshal.ReadIntPtr(GetProcAddress(lib, "_Version"));
		var ver2 = Marshal.PtrToStringAnsi(procAddress);

				throw new Win32Exception(Marshal.GetLastWin32Error());

private delegate String GetMyVersion();

[DllImport("kernel32", SetLastError = true, CharSet = CharSet.Unicode)]
static extern IntPtr LoadLibrary(string lpFileName);

[DllImport("kernel32", CharSet = CharSet.Ansi, ExactSpelling = true, SetLastError = true)]
static extern IntPtr GetProcAddress(IntPtr hModule, string procName);

[DllImport("kernel32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool FreeLibrary(IntPtr hModule);