Visual Studio 2022 C#ASP.NET WebForms具有服务参考文本文件(HTTP错误404.0)

发布于 2025-01-18 14:44:44 字数 1117 浏览 2 评论 0原文

我目前正在尝试使用带有服务引用的 Visual Studio 2022 ASP.NET Webforms 应用程序创建一个 Web 服务应用程序。目标是获取信息并将其作为文本文件存储在本地计算机上的项目文件夹中,以便本地服务器上的 Web 服务可以访问它。

我已成功创建文本文件并可以在本地计算机上访问它们,但是当我导航到本地服务器树上的文本文件时,我收到 HTTP 错误 404.0,如下所示。我需要访问我的服务器的任何用户都能够访问保存的文本文件。我尝试更改该文件夹和 web.config 文件中的安全权限,但没有成功。如果有人提出任何建议,我将不胜感激。

HTTP Error404 .0 屏幕截图

这是我将信息保存为文本文件的代码。

// 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 和本地文件路径如下:

  • URL https://localhost:44399/sx1l.txt
  • 路径名称 \\Mac\Home\Desktop\Homework3\Homework3\sx1l.txt

I am currently trying to create a web service application using Visual Studio 2022 ASP.NET Webforms application with a service reference. The goal is to take in information and store it as a text file on the local machine within the project folder so it is accessible by the web service on my local server.

I have successfully created the text files and can access them on my local machine, but when I navigate to the text file on my local server tree I get an HTTP Error 404.0 which is shown below. I need any user who accesses my server to be able to access the saved text files. I have tried to change security privileges on the folder and in my web.config file, but have not had any luck. I would appreciate any suggestions someone may have.

HTTP Error404.0 screenshot

Here is my code for where I save the information as a text file.

// 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);

Both my URL and local file path are the following:

  • URL https://localhost:44399/sx1l.txt
  • Path Name \\Mac\Home\Desktop\Homework3\Homework3\sx1l.txt

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

禾厶谷欠 2025-01-25 14:44:44

好的,因此您必须牢记文件映射如何与IIS一起使用。

您的代码背后:

那是平面Jane .NET代码。在大多数情况下,使用完整的Windows路径名称的任何代码,任何文件操作。就像编写桌面软件一样。在大多数情况下,这意味着背后的代码可以抓取/使用/查看计算机上的任何文件。

但是,实际上,当您使用运行ISS的完整爆炸的Web服务器(在VS和IIS Express开发过程中并没有真正做到这一点)?通常,出于安全原因,仅在WWWRoot文件夹中只能给Web服务器中的文件。

但是,您正在处理开发计算机 - 您将成为超级用户,并且(更重要的是)代码,因此可以读取/写入和抓取和使用计算机上的任何文件。

因此,在您的脑海中保持非常清楚:

代码=平面Jane Windows文件操作。

然后,我们从Web端(网页或您输入到Web浏览器中的URL)中有请求。

在这种情况下,文件仅映射到项目的根部,然后映射到sub文件夹。

因此,您可以上加载文件,然后将代码保存到计算机上的任何

位置 Web Project root文件

夹 并确保您使用VS添加该文件夹)

。 “>

i.sstatic.net/yfnxe.png“ rel =” nofollow noreferrer ,这将简单地在项目中创建一个子文件夹,您会看到这样的:

“在此处输入图像说明”

因此,文件夹必须位于根部,或者至少从root或base文件夹中开始是。

因此,对于上述,然后使用uploadfiles,然后将任何基于Web的路径名(URL)就是这样:(

https://localhost:44399/UpLoadFiles/sx1l.txt

假设我们将文件放在文件夹Uploadfiles中)。

但是,如果您想编写代码以触摸/使用/读取/保存并使用该文件?

您需要将上述URL转换为该平面Jane Windows路径名。 (对于背后的任何代码)。

因此,如果我想在代码中读取该文件名?

然后,我将使用server.mappath()翻译此URL。

说这样的东西:

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

我的意思是我不在乎,但是那个网络服务器代码可能在某些服务器上运行,而路径名称可能比以上更丑陋 - 但是我不在乎。

但是,从Web浏览器和Web服务器的观点(URL)中,上面看起来像这样:

https://localhost:44392/UpLoadFiles/sx1l.txt

在标记中,我可以说一个超级链接,例如:

        <a href="UpLoadFiles/sx1l.txt">UpLoadFiles/sx1l.txt</a>

因此,请保持Crystal的脑海中,以便在您的脑海中保持脑海名称。

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.

但是,您拥有大型网络附加的巨大网络存储计算机的情况如何说明PDF安全文件或零件图片目录会造成的船只呢?

好吧,那么您可以采用并使用我们所谓的“虚拟”文件夹。它们是在IIS Express中设置的痛苦,但是如果您使用IIS进行设置并运行要将网站发布到的最终服务器运行,则非常容易设置。

可以说,虚拟文件​​夹允许您将外部文件夹映射到侧面的根路径名中。

因此,您可能已经说了一个带有大量PDF Docuemnts的大服务器,

例如

\\corporate-server1\PDF\Documents

,在IIS中,您可以添加上述路径名称,例如称为PDF的文件夹。

这样说:

因此,当您执行上述内容时,该文件夹将像项目根部的任何平面Jane文件夹一样出现,但是文件路径可以并且将位于完整的不同位置在网站的wwwroot文件夹之外。

所以,现在我们最清楚了吗?

\\Mac\Home\Desktop\Homework3\Homework3\sx1l.txt

但是,您的代码具有以下内容:(

folderPath = Server.MapPath("~/Events");

File.WriteAllText(folderPath + eventFile, fullEventDetails);

您缺少上面的尾随“/”,您需要:

File.WriteAllText(folderPath + @"/" + eventFile, fullEventDetails);

因此,这意味着文本文件的URL将是:

https://localhost:44399/Events/sx1l.txt

如果您使用Visual Studio将文件添加到该文件夹​​(添加) - &gt;现有项目),然后确保您构建所有重建所有内容(否则,该文件将不会包含在IIS Express的Debug Run +启动IIS Express。

因此,鉴于您将保存到一个名为Events的文件夹中(作为Sub Folder(sub文件夹) wwwroot或HTE网站的基本文件夹,然后是您应该使用的URL,但是您的代码总是缺少该文件夹和文件名之间的“/”。

Ok, so you have to keep in mind how file mapping works with IIS.

Your code behind:

that is plane jane .net code. For the most part, any code, any file operations using full qualified windows path names. It like writing desktop software. For the most part, that means code behind can grab/use/look at any file on your computer.

However, in practice when you use a full blown web server running ISS (which you not really doing during development with VS and IIS express)? Often, for reasons of security, then ONLY files in the wwwroot folder is given permissions to the web server.

However, you working on your development computer - you are in a effect a super user, and you (and more important) your code thus as a result can read/write and grab and use ANY file on your computer.

So, keep above VERY clear in your mind:

Code behind = plane jane windows file operations.

Then we have requests from the web side of things (from a web page, or a URL you type into the web browser.

In that case, files are ONLY EVER mapped to the root of your project, and then sub folders.

So, you could up-load a file, and then with code behind save the file to ANY location on your computer.

However, web based file (urls) are ONLY ever mapped though the web site.

So, in effect, you have to consider your VS web project the root folder. And if you published to a real web server, that would be the case.

So, if you have the project folder, you can add a sub folder to that project.

Say, we add a folder called UpLoadFiles. (and make sure you use VS to add that folder). So we right click on the project and choose add->

So, you right click on the base project and add, like this:

enter image description here

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

enter image description here

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:

enter image description here

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文