框架: ASP.NET
假设我的网站位于 D:\Webhome 目录下。目录结构如下所示:
D:\Webhome
|
|-web.config
|-default.aspx
|-folder_1
| |- file1
|
|-folder_2
我在想,在web.config中引用文件时,使用绝对路径还是相对路径更好。我正在使用绝对路径,但是我的工作站环境与生产服务器不同,生产服务器上的网站位于E:\Web。因此,我只能将本地的web.config复制到生产服务器上。相反,我只需手动复制差异即可。
另一个问题是如何使用相对路径。例如:
<entry1 name="key1" file="~/folder1/file1">
<entry1 name="key1" file="folder1/file1">
<entry1 name="key1" file="~\folder1\file1">
<entry1 name="key1" file="folder1\file1">
有没有针对这个问题的MSDN或文档?请建议,谢谢。
编辑:
对我的情况似乎不起作用。我已经使用HttpContext.Server.MapPath(“〜/”)进行了检查,它显示为E:\ Webhome 。而我的xml位于
E:\ Webhome \ QueryDictionary \ ITEM.xml 。你能看出问题在哪里吗?
这是我的web.config文件
<queryDictionaries>
<queryDictionary name="ITEM" file="~/QueryDictionary/ITEM.xml" type="Com.Data.SqlServer"/>
</queryDictionaries>
这是我得到的错误信息:
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()
编辑2:
看起来在Com.Data.Query.QueryCommander.LoadDictionaries()
中,我必须使用HttpContext.Server.MapPath()
来解析文件路径以获取绝对路径。这就是原因吗?