framework: ASP.NET
Say I have my website located in D:\Webhome. The structure is like below
D:\Webhome
|
|-web.config
|-default.aspx
|-folder_1
| |- file1
|
|-folder_2
I was wondering is it better to use absolute or relative path in web.config to reference to files. I am using absolute path, but environment in my workstation is different in production server, which has website located in E:\Web. So I can just copy web.config on my local to production server. Instead, I just manually copy the difference over.
The other question is how to use relative path. For example:
<entry1 name="key1" file="~/folder1/file1">
<entry1 name="key1" file="folder1/file1">
<entry1 name="key1" file="~\folder1\file1">
<entry1 name="key1" file="folder1\file1">
Is there a msdn or document for this? Please advise, thanks.
EDIT:
It seems not work for my case. I've checked with HttpContext.Server.MapPath("~/") and it's showing E:\Webhome
. And my xml is at E:\Webhome\QueryDictionary\ITEM.xml
. Can you please see what's wrong?
This is my web.config
<queryDictionaries>
<queryDictionary name="ITEM" file="~/QueryDictionary/ITEM.xml" type="Com.Data.SqlServer"/>
</queryDictionaries>
This is the error I got:
System.IO.DirectoryNotFoundException: Could not find a part of the path 'C:\Program Files\Common Files\Microsoft Shared\DevServer\10.0\~\QueryDictionary\ITEM.xml'.
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath)
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize)
at System.Xml.XmlDownloadManager.GetStream(Uri uri, ICredentials credentials, IWebProxy proxy, RequestCachePolicy cachePolicy)
at System.Xml.XmlUrlResolver.GetEntity(Uri absoluteUri, String role, Type ofObjectToReturn)
at System.Xml.XmlTextReaderImpl.OpenUrlDelegate(Object xmlResolver)
at System.Threading.CompressedStack.runTryCode(Object userData)
at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData)
at System.Threading.CompressedStack.Run(CompressedStack compressedStack, ContextCallback callback, Object state)
at System.Xml.XmlTextReaderImpl.OpenUrl()
at System.Xml.XmlTextReaderImpl.Read()
at System.Xml.XmlLoader.Load(XmlDocument doc, XmlReader reader, Boolean preserveWhitespace)
at System.Xml.XmlDocument.Load(XmlReader reader)
at System.Xml.XmlDocument.Load(String filename)
at Com.Data.Query.QueryCommander.LoadDictionaries()
EDIT2:
Looks like in Com.Data.Query.QueryCommander.LoadDictionaries()
, I have to use HttpContext.Server.MapPath()
to parse the filepath to get the absolute path. Is that the reason?
If you're just linking pages that are all on the same website, traditional wisdom holds that there is no need to use absolute links. If you ever change your domain name, keeping all of your links relative will make the transition much easier.
An absolute path is defined as specifying the location of a file or directory from the root directory(/). In other words,we can say that an absolute path is a complete path from start of actual file system from / directory. Relative path. Relative path is defined as the path related to the present working directly(pwd) ...
The difference between relative and absolute paths is that when using relative paths you take as reference the current working directory while with absolute paths you refer to a certain, well known directory.
A relative path is a way to specify the location of a directory relative to another directory. For example, suppose your documents are in C:\Sample\Documents and your index is in C:\Sample\Index. The absolute path for the documents would be C:\Sample\Documents.
For the love of god use relative paths. Your little E:\web problem is the tip of the iceberg.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With