“找不到路径的一部分” C# IIS 在生产环境中的部署

发布于 2024-12-11 23:05:51 字数 528 浏览 3 评论 0原文

我正在从我的应用程序加载一个 xml 文件:

XDocument.Load(HttpContext.Current.Server.MapPath("/") + "XMLMetadata\\Actions.1.xml"); 

在开发环境中它工作正常。

但是我部署应用程序后,系统找不到它。
这是错误:
异常详细信息:System.IO.DirectoryNotFoundException:找不到路径“C:\inetpub\wwwroot\XMLMetadata\Actions.1.xml”的一部分。

该文件已部署到
C:\inetpub\wwwroot\MyApp\XMLMetadata\Actions.1.xml
而不是:
C:\inetpub\wwwroot\XMLMetadata\Actions.1.xml

ASP .NET 4 MVC 应用程序 我缺少什么?

I am loading an xml file from my applicatin:

XDocument.Load(HttpContext.Current.Server.MapPath("/") + "XMLMetadata\\Actions.1.xml"); 

In the dev environment it is working fine.

But After I deploy the application, the system cannot find it.
this is the error:
Exception Details: System.IO.DirectoryNotFoundException: Could not find a part of the path 'C:\inetpub\wwwroot\XMLMetadata\Actions.1.xml'.

the file was deployed to
C:\inetpub\wwwroot\MyApp\XMLMetadata\Actions.1.xml
and not to:
C:\inetpub\wwwroot\XMLMetadata\Actions.1.xml

ASP .NET 4 MVC APPLICATION
What am I missing?

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

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

发布评论

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

评论(6

淡淡的优雅 2024-12-18 23:05:51

嗯 - 您的意思是问为什么它部署到 C:\inetpub\wwwroot\MyApp\XMLMetadata\ 而不是 C:\inetpub\wwwroot\XMLMetadata\

事实上,您已经回答了出现错误的原因。

正是出于这个原因,您尝试使用引用 URL,即 ~\XMLMetadata\ 而不是硬编码位置。

还要确保您在本地使用 IIS 在开发系统上进行测试。

Hmm - do you mean to ask why it has deployed to C:\inetpub\wwwroot\MyApp\XMLMetadata\ rather than C:\inetpub\wwwroot\XMLMetadata\?

As it is you've answered the reason why you get an error.

It is for this reason that you try and use referential URLs i.e. ~\XMLMetadata\ rather than a hard coded location.

Also ensure that you test on the development system using IIS locally.

乜一 2024-12-18 23:05:51

我很确定您希望在这种情况下使用“~”。与 Server.MapPath 一起使用的“~”将为您提供应用程序根目录的物理路径。其中“/”将为您提供域名根的物理路径。这些路径可能会有所不同,具体取决于您的 IIS 设置方式。

XDocument.Load(HttpContext.Current.Server.MapPath("~") + "XMLMetadata\\Actions.1.xml"); 

I'm pretty sure you are looking to use the "~" in this case. The "~" used with Server.MapPath will give you the physical path to the root of the application. Where as "/" will give you the physical path to the root of the domain name. These paths can be different based on how you have IIS setup.

XDocument.Load(HttpContext.Current.Server.MapPath("~") + "XMLMetadata\\Actions.1.xml"); 
筑梦 2024-12-18 23:05:51

很难说你在这里使用的具体框架和基类是什么,但我猜是 ASP.NET。如果是这样,您可能需要查看 Control.ResolveUrl()

然后,您应该得到如下所示的内容,而不是 Load() 调用。

XDocument.Load(this.ResolveUrl("~/XMLMetadata\\Actions.1.xml"));

It's difficult to say what exact framework and base class you are working here, but I'm guessing ASP.NET. If so, you may want to have a look at Control.ResolveUrl().

Then, you should get something like the following instead of your Load() call.

XDocument.Load(this.ResolveUrl("~/XMLMetadata\\Actions.1.xml"));
没企图 2024-12-18 23:05:51

我过去使用过类似的东西:

    var appPath = HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority) + HttpContext.Current.Request.ApplicationPath;
    if (appPath.Substring(appPath.Length - 1, 1) != "/") appPath = appPath + "/";
    var filePath = appPath + "XMLMetaData//Actions.1.xml";

I've used something like this in the past:

    var appPath = HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority) + HttpContext.Current.Request.ApplicationPath;
    if (appPath.Substring(appPath.Length - 1, 1) != "/") appPath = appPath + "/";
    var filePath = appPath + "XMLMetaData//Actions.1.xml";
小嗷兮 2024-12-18 23:05:51

这是因为您的 Web 应用程序位于虚拟目录中,因此您需要解析到该级别:

var applicationRoot = 
    HttpContext.Current.Server.MapPath(
        HttpRequest.ApplicationPath);

来自 MSDN 的应用程序路径

使用此属性构造相对于应用程序根目录的 URL
来自不在根目录中的页面或 Web 用户控件。
这允许页面和共享控件存在于不同级别
目录结构使用相同的代码链接到资源
应用程序中的固定位置。

This is because your web application is sat in a virtual directory, so you will need to resolve to that level:

var applicationRoot = 
    HttpContext.Current.Server.MapPath(
        HttpRequest.ApplicationPath);

Remarks on ApplicationPath from MSDN:

Use this property to construct a URL relative to the application root
from a page or Web user control that is not in the root directory.
This allows pages and shared controls that exist at different levels
of a directory structure to use the same code to link to resources at
fixed locations in the application.

巴黎盛开的樱花 2024-12-18 23:05:51
XDocument.Load(HttpContext.Current.Server.MapPath("XMLMetadata/Actions.1.xml"));
XDocument.Load(HttpContext.Current.Server.MapPath("XMLMetadata/Actions.1.xml"));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文