在引用的库ASP.NET相对路径 [英] ASP.NET Relative Paths in Referenced Libraries

查看:116
本文介绍了在引用的库ASP.NET相对路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ASP.NET网站中,我加载从XML文件中的一些验证规则。此XML文件名,没有路径信息,是在一个库中硬codeD。 (我知道硬codeD的名字是不是很好,但我们只是用它去为这个例子)。

I have an ASP.NET website in which I am loading some validation rules from an xml file. This xml file name, with no path info, is hard coded in a library. (I know that the hard coded name is not good, but let's just go with it for this example).

当我运行的网站,ASP.NET尝试查找XML文件中的的路径,在C#文件中的名称是很难$ C $光盘。这是完全超乎想象到我,因为我无法捉摸如何,在运行时,我们甚至考虑一个源路径作为解决不合格的文件名的可能性。

When I run the website, ASP.NET tries to find the xml file in the source path, where the C# file in which name is hard coded is. This is completely mind boggling to me, as I can't fathom how, at runtime, we are even considering a source path as a possibility for resolving an unqualified filename.

// the config class, in C:\temp\Project.Core\Config.cs
public static string ValidationRulesFile {
   get { return m_validationRulesFile; }
} private static string m_validationRulesFile = "validation_rules.xml";

// using the file name
m_validationRules.LoadRulesFromXml( Config.ValidationRulesFile, "Call" );

下面是表示我们寻找路径中的异常是一样Config.cs:

Here is the exception showing the path we are looking in is the same as Config.cs:

  Exception Details: System.IO.FileNotFoundException: 
Could not find file 'C:\temp\Project.Core\validation_rules.xml'.

谁能解释这样对我?我已经知道你应该如何处理ASP.NET一般路径,所以请不要用解决方案做出回应。我真的想明白了这一点,因为它真的让我很吃惊,这是去打扰我没有尽头。

Can anyone explain this to me? I already know how you are supposed to handle paths in general in ASP.NET so please don't respond with solutions. I just really want to understand this, since it really surprised me, and It is going to bother me to no end.

下面是相关code为LoadRulesFromXml

Here is the relevant code for LoadRulesFromXml

public void LoadRulesFromXml( string in_xmlFileName, string in_type ) 
{   	
    XmlDocument xmlDoc = new XmlDocument();
    xmlDoc.Load( in_xmlFileName );
...

UPDATE2

它看起来像卡西尼Web服务器获取其当前的目录中VS设置,确实它设置为我的库项目的路径。我不知道究竟VS如何确定哪些项目要使用的路径,但是这至少说明发生了什么。感谢乔。

UPDATE2

It looks like the Cassini web server gets its current directory set by VS, and indeed it is set to the path of my library project. I'm not sure exactly how VS determines which project to use for the path, but this at least explains what is happening. Thanks Joe.

推荐答案

如果您不提供路径,则文件访问通常会使用当前的工作目录作为默认值。在ASP.NET这可能是你的web应用程序目录。

If you don't supply a path, then file access will normally use the current working directory as the default. In ASP.NET this is probably your web application directory.

这不是通常依赖于当前的工作目录是个好主意,所以你可以使用Path.Combine指定不同的默认目录,例如1相对AppDomain.CurrentDomain.BaseDirectory,这也是一个ASP.NET应用程序Web应用程序目录。

It's not usually a good idea to rely on the current working directory, so you can use Path.Combine to specify a different default directory, e.g. one relative to AppDomain.CurrentDomain.BaseDirectory, which is also the web application directory for an ASP.NET app.

您应该明确地添加到您打开该文件的名称的路径。您也可以尝试跟踪当前工作目录。

You should add the path explicitly to the name of the file you're opening. You could also try tracing the current working directory.

当从Visual Studio运行卡西尼​​号,当前目录是从什么恰好是Visual Studio的工作目录继承:这似乎是你的情况

When running Cassini from Visual Studio, the current directory is inherited from whatever happens to be Visual Studio's working directory: this seems to be your case.

即:

public void LoadRulesFromXml( string in_xmlFileName, string in_type ) 
{   
    // To see what's going on
    Debug.WriteLine("Current directory is " +
              System.Environment.CurrentDirectory);    

    XmlDocument xmlDoc = new XmlDocument();    

    // Use an explicit path
    xmlDoc.Load( 
       System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory,
       in_xmlFileName) 
    );
...

这篇关于在引用的库ASP.NET相对路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆