Absolute or Relative File Path in web.config
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 showingE:\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 ofOb开发者_StackOverflow社区jectToReturn)
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?For the love of god use relative paths. Your little E:\web problem is the tip of the iceberg.
http://msdn.microsoft.com/en-us/library/ms178116.aspx
A relative path will start you at your current page in your site, so you don't have to worry about anything else before that! Which allows you to move your web site around and not worry about changing your file paths.
For example, if you want to access file1 (say is an image file) from your default.aspx, you would say:
<img src="folder_1/file1.jpg" />
However, in your web.config file you will want to specify the root of your website using a tilda and forward slash:
<authentication mode="Forms">
<forms loginUrl="~/index.aspx" name="adAuthCookie" path="/">
</forms>
</authentication>
Hope this is what you're looking for, gl
HttpContext.Server.MapPath("~/folder_1/file1") gives you back the path of the physical file on the maching hosting the web site.
精彩评论