0

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 :

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

but I can't use The "Request" object from separated C# file ?? 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 ?? thx

4 Answers 4

2

I would recommend you passing the path to your library from the web application. 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:

var path = Path.Combine(
    HostingEnvironment.ApplicationPhysicalPath, 
    "filename.txt"
);
Sign up to request clarification or add additional context in comments.

4 Comments

thanks Darin But I faced the same problem I can't use "Server" object from my C# file (this is my basic problem)
The first snippet I provided is to be placed in the Web Application where you have the Server object and not in the class library. You pass the path to the class library method as parameter so it won't need to calculate it.
thx man The "HostingEnvironment.ApplicationPhysicalPath" is what I was looking for
@Hany, please don't do this in your class library. I provided this just as an example but advice you against it.
1

Using HttpContext.Current you have access to Request, Server, Response and other objects of the HTTP request.

2 Comments

Blech! It will also make the class nearly impossible to test.
@tvanfosson, I would say impossible :-)
1

but I can't use The "Request" object from separated C# file ??

I'm guessing you mean this is in a dll? If so, then you can get to it by referencing system.web in the separate dll, and getting at the httpcontext.current object

Comments

0

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.

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.