我目前正在嘗試使用帶有服務參考的 Visual Studio 2022 ASP.NET Webforms 應用程式創建一個 Web 服務應用程式。目標是獲取資訊并將其作為文本檔案存盤在專案檔案夾中的本地計算機上,以便我的本地服務器上的 Web 服務可以訪問它。
我已經成功創建了文本檔案,并且可以在本地計算機上訪問它們,但是當我導航到本地服務器樹上的文本檔案時,我得到一個 HTTP 錯誤 404.0,如下所示。我需要任何訪問我的服務器的用戶都能夠訪問保存的文本檔案。我試圖更改檔案夾和web.config檔案中的安全權限,但沒有任何運氣。我將不勝感激有人可能提出的任何建議。

這是我將資訊保存為文本檔案的代碼。
// Randomly generate string for text file name
var chars = "abcdefghijklmnopqrstuvwxyz0123456789";
var textFile = new char[4];
var random = new Random();
for (int i = 0; i < textFile.Length; i )
{
textFile[i] = chars[random.Next(chars.Length)];
}
eventFile = "\\";
eventFile = new String(textFile);
eventFile = ".txt";
folderPath = Server.MapPath("~/Events");
File.WriteAllText(folderPath eventFile, fullEventDetails);
我的 URL 和本地檔案路徑如下:
- 網址
https://localhost:44399/sx1l.txt - 路徑名稱
\\Mac\Home\Desktop\Homework3\Homework3\sx1l.txt
uj5u.com熱心網友回復:
好的,所以您必須記住檔案映射如何與 IIS 一起作業。
您背后的代碼:
那是飛機簡.net代碼。大多數情況下,任何代碼、任何檔案操作都使用完全限定的 Windows 路徑名。它喜歡撰寫桌面軟體。在大多數情況下,這意味著后面的代碼可以抓取/使用/查看您計算機上的任何檔案。
但是,在實踐中,當您使用運行 ISS 的完整 Web 服務器時(在使用 VS 和 IIS express 開發期間您并沒有真正這樣做)?通常,出于安全原因,只有 wwwroot 檔案夾中的檔案才被授予訪問 Web 服務器的權限。
然而,你在你的開發計算機上作業——你實際上是一個超級用戶,你(更重要的是)你的代碼因此可以讀/寫,抓取和使用你計算機上的任何檔案。
因此,請記住非常清楚:
代碼后面=平面簡windows檔案操作。
然后我們收到來自 Web 端的請求(來自網頁或您在 Web 瀏覽器中鍵入的 URL。
在這種情況下,檔案只會映射到專案的根目錄,然后是子檔案夾。
因此,您可以上傳檔案,然后使用后面的代碼將檔案保存到計算機上的任何位置。
但是,基于 Web 的檔案 (url) 只能通過網站映射。
因此,實際上,您必須將 VS Web 專案視為根檔案夾。如果您發布到真正的 Web 服務器,情況就是如此。
因此,如果您有專案檔案夾,則可以向該專案添加一個子檔案夾。
比如說,我們添加了一個名為 UpLoadFiles 的檔案夾。(并確保您使用 VS 添加該檔案夾)。所以我們右鍵點擊專案,選擇add->
因此,您右鍵單擊基礎專案并添加,如下所示:

So, that will simple create a sub folder in your project, you see it like this:

So, the folder MUST be in the root, or at the very least start in the root or base folder your project is.
So, for above, then with UpLoadFiles, then any WEB based path name (url) will be this:
https://localhost:44399/UpLoadFiles/sx1l.txt
(assuming we put the file in folder UpLoadFiles).
But, if you want to write code to touch/use/read/save and work with that file?
You need to translate the above url into that plane jane windows path name. (for ANY code behind).
So, if I want to in code read that file name?
Then I would use Server.MapPath() to translate this url.
say somthing like this:
string strFileName = "sx1l.txt";
string strFolderName = "UpLoadFiles"
string strInternaleFileName = server.MapPath(@"~/" strFolderNme @"/" sx1l.txt";
// ok, so now we have the plane jane windows file name. It will resolve to something like say this:
C:\Users\AlbertKallal\source\repos\MyCalendar\UpLoadFiles\sx1l.txt
I mean I don't really care, but that web server code could be running on some server and that path name could be even more ugly then above - but me the developer don't care.
but, from a web browser and web server point of view (URL), then above would look like this:
https://localhost:44392/UpLoadFiles/sx1l.txt
And in markup, I could drop in say a hyper link such as:
<a href="UpLoadFiles/sx1l.txt">UpLoadFiles/sx1l.txt</a>
So, keep CRYSTAL clear in your mind with working with path names.
Web based URL, or markup = relative path name, ONLY root or sub folders allowed
code behind: ALWAYS will use a plane jane full windows standard file and path.
But, what about the case where you have big huge network attached storage computer - say will a boatload of PDF safety documents, or a catalog of part pictures?
Well, then you can adopt and use what we call a "virtual" folder. They are pain to setup in IIS express, but REALLY easy to setup if you using IIS to setup and run the final server where you going to publish the site to.
Suffice to say, a virtual folder allows you to map a EXTERNAL folder into the root path name of your side.
So, you might have say a big server with a large number of PDF docuemnts,
say on
\\corporate-server1\PDF\Documents
so, in IIS, you can add the above path name, say as a folder called PDF.
Say like this:

So, WHEN you do the above, then the folder will appear like any plane jane folder in the root of the project, but the file paths can and will be on a complete different location OUTSIDE of the wwwroot folder for the web site.
So, now that we have the above all clear?
\\Mac\Home\Desktop\Homework3\Homework3\sx1l.txt
But, your code has this:
folderPath = Server.MapPath("~/Events");
File.WriteAllText(folderPath eventFile, fullEventDetails);
(you missing the trailing "/" in above, you need this:
File.WriteAllText(folderPath @"/" eventFile, fullEventDetails);
So, that means the url for the text file will then be:
https://localhost:44399/Events/sx1l.txt
And if you using Visual Studio to add files to that folder (add->existing items), then MAKE SURE you Build->rebuild all (else the file will not be included in the debug run launching of IIS express.
So, given that you saving into a folder called Events (as sub folder of wwwroot, or your base folder for hte web site, then the above is the url you should use, but your code always was missing that "/" between folder and file name.
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/456831.html
