简体   繁体   English

ASPX - web 站点中我的文件夹的路径是什么?

[英]ASPX - what is the path to my folder in web site?

I have a WebSite project - ASPX, 4.0.我有一个网站项目 - ASPX,4.0。 In it I have a folder like:在其中我有一个文件夹,如:

bin\Xslt\Template.xslt

I want to load this file in my class library.我想将此文件加载到我的 class 库中。 In web.config is:在 web.config 中是:

 <configuration>
    <appSettings>
        <add key="Filepath" value=".\Xslt\Template.xslt" />
    </appSettings>
 </configuration>

But, my class library can't find it:但是,我的 class 库找不到它:

 mTransform = new XslCompiledTransform();
 mTransform.Load(ConfigurationManager.AppSettings["Filepath"]);

throws: Could not find a part of the path 'C:\Xslt\Template.xslt'.抛出: Could not find a part of the path 'C:\Xslt\Template.xslt'. I know there's a catch here, but I can't remember the proper way...我知道这里有一个陷阱,但我不记得正确的方法......

How to reference a file in an aspx \bin\ folder correctly?如何正确引用 aspx \bin\ 文件夹中的文件?

Use Server.Mappath("~\xslt\template.xslt");使用Server.Mappath("~\xslt\template.xslt");

Try changing your key to:尝试将您的密钥更改为:

<add key="Filepath" value="~/bin/Xslt/Template.xslt" />

And change your code to:并将您的代码更改为:

mTransform.Load(HttpContext.Current.Server.MapPath(ConfigurationManager.AppSettings["Filepath"]));

Try to give the complete path in your web config Example if your file is at C:\VSPROJECTS\bin\Xslt\Template.xslt尝试在 web 配置中提供完整路径 例如,如果您的文件位于 C:\VSPROJECTS\bin\Xslt\Template.xslt

Then in your webconfig write it as然后在你的webconfig中写成
< add key="Filepath" value="C:\VSPROJECTS\bin\Xslt\Template.xslt" /> < add key="文件路径" value="C:\VSPROJECTS\bin\Xslt\Template.xslt" />

I found the way (since it's in an assembly and I didn't want to add dependency on System.Web):我找到了方法(因为它在程序集中,我不想添加对 System.Web 的依赖):

string codeBase = Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase);
string filepathRelative = ConfigurationManager.AppSettings["Filepath"];

string fullFilepath = Path.Combine(codeBase, filepathRelative);

Thanks谢谢

edit : The System.AppDomain.CurrentDomain.BaseDirectory also does the trick...编辑System.AppDomain.CurrentDomain.BaseDirectory也可以解决问题...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM