简体   繁体   English

ASP.net中的路径问题

[英]Path Problem in ASP.net

I am trying to do the following I am building asp.net website with c# language I want to read a text file from my project(the file is inside the project) I tried to get the file Path by this way : 我正在尝试执行以下操作我正在使用c#语言构建asp.net网站,我想从项目中读取文本文件(该文件位于项目内部),我试图通过这种方式获取文件Path:

string path=Request.PhysicalApplicationPath+"filename.txt";

but I can't use The "Request" object from separated C# file ?? 但我不能使用单独的C#文件中的“请求”对象? note: separated C3 file,I mean it's not related with aspx file can you help me with my way or do you have another way ?? 注意:分开的C3文件,我的意思是它与aspx文件不相关,您可以用我的方式还是以其他方式帮助我? thx 谢谢

I would recommend you passing the path to your library from the web application. 我建议您从Web应用程序将路径传递到库。 So for example in your web app: 因此,例如在您的网络应用中:

var path = Server.MapPath("~/filename.txt");
var result = BusinessLayer.SomeMethod(path);

You could also use HostingEnvironment in your class library but I would really advice you against it as it creates a dependency with System.Web which makes your class library tied to a web context and not unit testable friendly: 您也可以在类库中使用HostingEnvironment ,但是我真的建议您反对它,因为它会与System.Web创建依赖关系,这会使您的类库与Web上下文绑定在一起,而不是可单元友好的:

var path = Path.Combine(
    HostingEnvironment.ApplicationPhysicalPath, 
    "filename.txt"
);

使用HttpContext.Current可以访问HTTP请求的Request,Server,Response和其他对象。

but I can't use The "Request" object from separated C# file ?? 但我不能使用单独的C#文件中的“请求”对象?

I'm guessing you mean this is in a dll? 我猜你的意思是这在dll中? If so, then you can get to it by referencing system.web in the separate dll, and getting at the httpcontext.current object 如果是这样,则可以通过在单独的dll中引用system.web并获取httpcontext.current对象来进行访问。

I would use some sort of injection mechanism either to give the application root path to the class or a copy of the current context/request to the class that it can use. 我将使用某种注入机制,要么将应用程序的根路径提供给类,要么将当前上下文/请求的副本提供给它可以使用的类。 Essentially, you want to give the class the means to find the path (or even give it the path) rather than use a fixed dependency that is hard to recreate in testing. 本质上,您想给类提供一种找到路径(甚至给它提供路径)的方法,而不是使用在测试中很难重新创建的固定依赖关系。 To simplify my example, I'll use the Request as you are doing, though, you could easily provide just the base path of the application as a string as well. 为了简化我的示例,我将在执行操作时使用Request,但是您也可以轻松地仅将应用程序的基本路径作为字符串提供。

public class Foo
{
     // HttpRequestBase may be more appropriate
     private HttpRequest Request { get; set; }

     public Foo( HttpRequest request )
     {
         this.Request = request;
     }

     public void Bar()
     {
          string path = Path.Combine( this.Request.PhysicalApplicationPath,
                                      "filename.txt" );
          ...
     }
}

Note that you could combine this with @Darin's ideas on how to calculate the server path as well. 请注意,您也可以将其与@Darin关于如何计算服务器路径的想法结合起来。

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

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