2011-02-17 46 views
5

框架:ASP.NET絕對或相對文件路徑在web.config中

說我有我的網站位於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() 



EDIT2:
看起來像Com.Data.Query.QueryCommander.LoadDictionaries(),我必須使用HttpContext.Server.MapPath()解析文件路徑以獲取絕對路徑。這是原因嗎?

回答

3

http://msdn.microsoft.com/en-us/library/ms178116.aspx
相對路徑將啓動您在您網站的當前頁面,所以您不必擔心其他任何事情!這使您可以移動您的網站,而不用擔心更改文件路徑。 例如,如果要訪問文件1(說是圖像文件)從您的Default.aspx,你會說:

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


然而,在你的web.config文件,你會要指定根使用蒂爾達和斜線您的網站:

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


希望這是你在找什麼,GL

10

爲了上帝的愛使用相對路徑。你的小E:\ web問題是冰山一角。

+0

阿門那! :D –

+1

آمين(阿拉伯語中的阿門),這是非常真實的,因爲絕對路徑之後會產生問題。 –

1

HttpContext.Server.MapPath(「〜/ folder_1/file1」)爲您提供託管網站的機器上物理文件的路徑。