This article is a mirror article of machine translation, please click here to jump to the original article.

View: 14933|Reply: 1

[ASP.NET] asp.net Get site directory file is empty

[Copy link]
Posted on 1/5/2018 4:18:49 PM | | | |
First, paste the code to get the website directory file, as follows:



After running this sentence, an exception will be thrown saying that the object is empty, as shown in the figure below:



Why do I get null?

Because the thread running this code is not the current thread that handles the current request

Why is it not null when you get it elsewhere?

This is because ASP.NET program has already set the HttpContext object to the System.Runtime.Remoting.Messaging.CallContext.HostContext property mentioned earlier before calling your code.

HttpApplication has an internal method OnThreadEnter(), ASP. .NET calls this method to switch the HttpContext before calling external code, for example, whenever the pipeline's event handler is executed, or when the synchronization context (AspNetSynchronizationContext) executes a callback. After switching the thread's CallContext.HostContext property, our code can access the HttpContext reference. Note: The reference to the HttpContext is actually saved in the HttpApplication object.



How to get the absolute path of the file in this case?

We can access System.Web.HttpRuntime.AppDomainAppPath to get the root path of the program, and then splice the relative paths of the file





Previous:Complete explanation of the underlying vulnerability of Intel CPUs: No mobile phone/computer in the world is spared
Next:Summary of the autofac creation instance methodology
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com