web.config中的绝对路径或相对路径

5

框架: 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()来解析文件路径以获取绝对路径。这就是原因吗?

3个回答

10

天啊,请使用相对路径。你的小错误 E:\web 只是冰山一角。


1
“آمين”(阿拉伯语中的Amen),这非常正确,因为绝对路径之后会出现问题。 - Owais Qureshi

3

http://msdn.microsoft.com/zh-cn/library/ms178116.aspx
相对路径会从您的网站当前页面开始,因此您不必担心路径之前的任何内容!这使您可以移动您的网站而不必担心更改文件路径。 例如,如果您想从default.aspx访问file1(比如一个图像文件),您应该这样说:

<img src="folder_1/file1.jpg" />


然而,在您的web.config文件中,您需要使用波浪线和正斜杠来指定您网站的根目录:

<authentication mode="Forms">
  <forms loginUrl="~/index.aspx" name="adAuthCookie" path="/">
  </forms>
</authentication>

希望这是您在寻找的内容,祝好运。

1

HttpContext.Server.MapPath("~/folder_1/file1") 会返回托管网站的物理文件在机器上的路径。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接