HttpRequest, HttpRuntime, AppDomain and friends()
看这里吧:http://duartes.org/gustavo/articles/Asp.net-Runtime-Cheat-Sheet-HttpRequest-HttpRuntime.aspx
ASP.NET Runtime Cheat Sheet: HttpRequest, HttpRuntime, AppDomain and friends
Last modified on Tuesday, 19 February 2008 at 17:07 UTCThis article has cheat sheets for retrieving various bits of ASP.NET runtime information. For each bit of info, there is:
- A code snippet for retrieving it in a page, with a link to MSDN
- Description, sometimes explanation
The value for the live ASP.NET app backing this siteThe values are static right now. They were obtained from a request to http://minuteman.duartes.org/gustavo/articles/Asp.net-Runtime-Cheat-Sheet-HttpRequest-HttpRuntime.aspx?param1=foo¶m2=bar
The code for the ASP.NET user control that generates the cheat sheets is in AspNetRuntimeDiagnostics.ascx.
HttpRequest information
HttpRequest.LogonUserIdentity and Process
When troubleshooting ASP.NET problems you often ask two questions: which process is the code running in?; and which Windows user is it running as? The default setups are thus:
Windows + IIS | Worker Process | ASP.NET threads |
---|---|---|
Vista + IIS 7.0 | w3wp.exe running as NETWORK SERVICE, one per application pool | IUSR, well-known account with SID S-1-5-17, see this post |
Windows 2003 + IIS 6.0 | w3wp.exe running as NETWORK SERVICE, one per application pool | IUSR_MACHINE_NAME, per-machine anonymous IIS user |
Windows 2000/XP + IIS 5.0 | aspnet_wp.exe, one for all ASP.NET applications | ASPNET, per-machine ASP.NET worker account |
These defaults however are subject to many factors, so often you need to find the answers yourself. The cheat sheet below shows you the relevant classes and properties to use. ProcessMonitor is a great tool for troubleshooting Windows runtime problems, especially permission and access denied issues. If you wish to read up on security and .NET, see Keith Brown's superb online book, The .NET Developer's Guide to Windows Security.
C# Page snippet + description | Value |
---|---|
Process.GetCurrentProcess().MainModule.FileName GetCurrentProcess() has security demands and may not be available in certain situations. One work-around is to use Environment.GetCommandLineArgs()[0] to at least get the file name |
c:\windows\system32\inetsrv\w3wp.exe |
Process.GetCurrentProcess().Id The Windows process ID for the process running your code. This is the process you attach to for debugging, watch for perf monitoring, and kill to restart your app 'cold'. |
3008 |
File: c:\windows\system32\inetsrv\w3wp.exe File version: 6.0.3790.3959 (srv03_sp2_rtm.070216-1710) FileDescription: IIS Worker Process Product: Internet Information Services | |
12/24/2008 3:09:55 AM | |
this.Request.LogonUserIdentity.Groups These are the Windows groups which contain the user running your ASP.NET code. |
MINUTEMAN\None Everyone BUILTIN\Guests BUILTIN\Users NT AUTHORITY\NETWORK NT AUTHORITY\Authenticated Users NT AUTHORITY\This Organization NT AUTHORITY\NTLM Authentication |
this.Request.LogonUserIdentity.Name This is the Windows user running your ASP.NET code. This user should be granted the permissions needed by your application. |
MINUTEMAN\IUSR_MINUTEMAN |
this.Request.LogonUserIdentity.ImpersonationLevel This is 'Impersonation' if the thread running your code is impersonating a Windows user, otherwise it's 'None'. In IIS 6 and 7, this is normally 'Impersonation' because the ASP.NET threads run under a different user than the IIS worker process. |
Impersonation |
False | |
this.Request.LogonUserIdentity.IsSystem True if your code is running as Local System. This should never be the case. |
False |
this.Request.LogonUserIdentity.User.Value The SID for the account. |
S-1-5-21-433945606-595587905-2999028010-1013 |
Paths
Path names in ASP.NET may be confusing, but the concepts are simple:
- Physical paths refer to the server's filesystem (e.g., c:\inetpub\wwwroot\index.aspx).
- Virtual paths refer to HTTP paths, the stuff in the browser address bar. They come in two types: absolute and relative.
- Absolute virtual paths are exactly what you see in the browser, starting with the / after the host name (e.g., /gustavo/articles).
- Relative (or app-relative) virtual paths start with ~/. The ~/ is magic and always represents the root virtual path of your application. So for an IIS application deployed at /bunny/love/ ASP.NET transforms ~/ into /bunny/love/. If the app is deployed to the IIS root (/), then ~/ simply becomes /.
There are two useful ASP.NET tools to manipulate paths. The HttpServerUtility.MapPath method converts a virtual path to a physical path. The VirtualPathUtility class has several methods for manipulating virtual paths.
C# Page snippet + description | Value |
---|---|
Gets the IIS root virtual path for your ASP.net application. ASP.NET transforms ~/ into this value when making an absolute virtual path. | / |
this.Request.AppRelativeCurrentExecutionFilePath Returns the app-relative virtual path for the HttpHandler currently executing, normally your page. Equivalent to this.Page.AppRelativeVirtualPath |
~/gustavo/articles/Asp.net-Runtime-Cheat-Sheet-HttpRequest-HttpRuntime.aspx |
this.Request.CurrentExecutionFilePath Returns the absolute virtual path for the HttpHandler currently executing, normally your page. |
/gustavo/articles/Asp.net-Runtime-Cheat-Sheet-HttpRequest-HttpRuntime.aspx |
this.Request.PhysicalApplicationPath The root of your application in the server's file system. |
c:\inetpub\wwwroot\Duartes\ |
The file system location for the HttpHandler currently executing, normally your page. | c:\inetpub\wwwroot\Duartes\gustavo\articles\Asp.net-Runtime-Cheat-Sheet-HttpRequest-HttpRuntime.aspx |
/gustavo/articles |
Click here for an example where the ApplicationPath is not /.
Request.Url
C# Page snippet + description | Value |
---|---|
/gustavo/articles/Asp.net-Runtime-Cheat-Sheet-HttpRequest-HttpRuntime.aspx | |
http://minuteman.duartes.org/gustavo/articles/Asp.net-Runtime-Cheat-Sheet-HttpRequest-HttpRuntime.aspx?param1=foo¶m2=bar | |
minuteman.duartes.org | |
minuteman.duartes.org | |
minuteman.duartes.org | |
Dns | |
/gustavo/articles/Asp.net-Runtime-Cheat-Sheet-HttpRequest-HttpRuntime.aspx | |
http://minuteman.duartes.org:80/gustavo/articles/Asp.net-Runtime-Cheat-Sheet-HttpRequest-HttpRuntime.aspx?param1=foo¶m2=bar | |
/gustavo/articles/Asp.net-Runtime-Cheat-Sheet-HttpRequest-HttpRuntime.aspx?param1=foo¶m2=bar | |
80 | |
?param1=foo¶m2=bar | |
http | |
[0]: / [1]: gustavo/ [2]: articles/ [3]: Asp.net-Runtime-Cheat-Sheet-HttpRequest-HttpRuntime.aspx | |
Information from the HTTP request headers
C# Page snippet + description | Value |
---|---|
Used with ASP.NET anonymous profiles. | null |
Length in bytes for the BODY in the client's request. For a GET request, this is normally zero as there is no body. For a POST request it depends on how much data is sent to the server. | 0 |
This is usually equal to ContentLength, but it could be slightly different depending on how the client encoded its HTTP request. | 0 |
The URL that sent the user your way. Bless them. | null |
The client's browser. | Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.0.5) Gecko/2008120122 Firefox/3.0.5 |
10.1.10.150 | |
10.1.10.150 | |
User-configured language preferences sent by the browser. You might tune your content based on this. | [0]: en-us [1]: en;q=0.5 |
Environment
C# Page snippet + description | Value |
---|---|
Microsoft Windows NT 5.2.3790 Service Pack 2 | |
Amount of physical memory used by the ASP.net worker process | 149577728 |
Thread.CurrentThread.CurrentCulture Used by string formatting and other culture-sensitive methods. |
en-US |
Thread.CurrentThread.CurrentUICulture Used by the ResourceManager to load culture-specific resources like strings and images . |
en-US |
NT AUTHORITY | |
Do not trust this value. Stick to HttpRequest.LogonUserIdentity | NETWORK SERVICE |
.NET Framework version. This returns '2.0' even for assemblies compiled as .NET 3.5. | 2.0.50727.1433 |
HttpContext
C# Page snippet + description | Value |
---|---|
False | |
False | |
12/24/2008 3:57:45 AM |
HttpRuntime and AppDomain
Your ASP.NET code runs inside a .NET AppDomain, which in turn runs inside a Windows process. The IIS version and configurations determine where your AppDomain will run. IIS 6 and 7 by default run AppDomains in a Worker Process called w3wp.exe. There is one worker process for each IIS application pool. Inside the Worker Process, there is one AppDomain for each application in the pool. Here are some useful tidbits on the current AppDomain and process:
C# Page snippet + description | Value |
---|---|
AppDomain.CurrentDomain.SetupInformation.ConfigurationFile Configuration file for the current AppDomain. For ASP.net this is the web.config file. |
c:\inetpub\wwwroot\Duartes\web.config |
The /LM/W3SVC part is fixed for a given IIS instance. The following number is the Site ID within IIS. The last piece is the IIS root virtual path for the application (or 'ROOT' for /). | /LM/W3SVC/1/ROOT |
The filesystem folder containing your application. Equivalent to AppDomain.CurrentDomain.BaseDirectory | c:\inetpub\wwwroot\Duartes\ |
/ | |
Equivalent to AppDomain.CurrentDomain.FriendlyName | /LM/W3SVC/1/ROOT-1-128745617956562500 |
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\asp.netclientfiles | |
/aspnet_client/system_web/2_0_50727 | |
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\ | |
Equivalent to AppDomain.CurrentDomain.RelativeSearchPath | c:\inetpub\wwwroot\Duartes\bin\ |
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\ | |
This is where ASP.NET stores generated source files for your pages, controls, etc. Watch this folder to see what a processed page becomes. Pages are compiled to DLLs which are also stored here. Equivalent to AppDomain.CurrentDomain.DynamicDirectory | C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\root\1943e657\37080f43 |
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Config |
Loaded Assemblies
.NET code is packaged into .NET assemblies. It is often important to know which assemblies are loaded into your ASP.NET AppDomain because many problems boil down to missing or incorrect assemblies. The method AppDomain.CurrentDomain.GetAssemblies returns a list of assemblies loaded in your AppDomain (it requires full trust). Here's the output for the current domain:
Count of loaded assemblies: 27
Assemblies |
---|
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\mscorlib.dll mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 |
C:\WINDOWS\assembly\GAC_32\System.Web\2.0.0.0__b03f5f7f11d50a3a\System.Web.dll System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a |
C:\WINDOWS\assembly\GAC_MSIL\System\2.0.0.0__b77a5c561934e089\System.dll System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 |
C:\WINDOWS\assembly\GAC_MSIL\System.Configuration\2.0.0.0__b03f5f7f11d50a3a\System.Configuration.dll System.Configuration, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a |
C:\WINDOWS\assembly\GAC_MSIL\System.Xml\2.0.0.0__b77a5c561934e089\System.Xml.dll System.Xml, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 |
C:\WINDOWS\assembly\GAC_MSIL\Microsoft.JScript\8.0.0.0__b03f5f7f11d50a3a\Microsoft.JScript.dll Microsoft.JScript, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a |
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\root\1943e657\37080f43\App_global.asax.2xsiz5uk.dll App_global.asax.2xsiz5uk, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null |
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\root\1943e657\37080f43\assembly\dl3\b59cf41d\50132709_c073c801\WebSite.DLL WebSite, Version=0.1.802.2002, Culture=neutral, PublicKeyToken=null |
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\root\1943e657\37080f43\assembly\dl3\e477bdd8\14e5df28_726cc801\Common.Helpers.DLL Common.Helpers, Version=0.1.0.0, Culture=neutral, PublicKeyToken=null |
C:\WINDOWS\assembly\GAC_MSIL\System.Web.Mobile\2.0.0.0__b03f5f7f11d50a3a\System.Web.Mobile.dll System.Web.Mobile, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a |
C:\WINDOWS\assembly\GAC_MSIL\System.ServiceModel\3.0.0.0__b77a5c561934e089\System.ServiceModel.dll System.ServiceModel, Version=3.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 |
C:\WINDOWS\assembly\GAC_MSIL\System.Web.Extensions\3.5.0.0__31bf3856ad364e35\System.Web.Extensions.dll System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35 |
C:\WINDOWS\assembly\GAC_MSIL\System.Core\3.5.0.0__b77a5c561934e089\System.Core.dll System.Core, Version=3.5.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 |
C:\WINDOWS\assembly\GAC_32\System.Data\2.0.0.0__b77a5c561934e089\System.Data.dll System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 |
C:\WINDOWS\assembly\GAC_32\System.Transactions\2.0.0.0__b77a5c561934e089\System.Transactions.dll System.Transactions, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 |
C:\WINDOWS\assembly\GAC_32\System.EnterpriseServices\2.0.0.0__b03f5f7f11d50a3a\System.EnterpriseServices.dll System.EnterpriseServices, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a |
C:\WINDOWS\assembly\GAC_MSIL\SMDiagnostics\3.0.0.0__b77a5c561934e089\SMDiagnostics.dll SMDiagnostics, Version=3.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 |
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\root\1943e657\37080f43\App_Web_ghr_5vya.dll App_Web_ghr_5vya, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null |
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\root\1943e657\37080f43\App_Web_z-opz3of.dll App_Web_z-opz3of, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null |
C:\WINDOWS\assembly\GAC_MSIL\System.Web.RegularExpressions\2.0.0.0__b03f5f7f11d50a3a\System.Web.RegularExpressions.dll System.Web.RegularExpressions, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a |
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\root\1943e657\37080f43\App_Web_jzwzgh0y.dll App_Web_jzwzgh0y, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null |
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\root\1943e657\37080f43\App_Web_goxujwqv.dll App_Web_goxujwqv, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null |
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\root\1943e657\37080f43\assembly\dl3\c0be40b0\30f22d7a_a06bc801\Iris.DLL Iris, Version=0.1.712.1401, Culture=neutral, PublicKeyToken=null |
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\root\1943e657\37080f43\App_Web_l1fsy2rr.dll App_Web_l1fsy2rr, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null |
C:\WINDOWS\assembly\GAC_MSIL\System.Drawing\2.0.0.0__b03f5f7f11d50a3a\System.Drawing.dll System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a |
C:\WINDOWS\assembly\GAC_MSIL\System.Data.Linq\3.5.0.0__b77a5c561934e089\System.Data.Linq.dll System.Data.Linq, Version=3.5.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 |
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\root\1943e657\37080f43\assembly\dl3\740e21a0\8a54307a_a06bc801\Newtonsoft.Json.DLL Newtonsoft.Json, Version=1.3.0.0, Culture=neutral, PublicKeyToken=null |