Using Server.MapPath in external C# Classes in ASP.NET
Asked Answered
E

9

140

I'm trying to get the absolute path of certain files in a C# class. Server.MapPath works great of course for ASPX and their code-behind pages, but that doesn't exist in another class file. I tried HostingEnvironment.MapPath(), but that complains that the relative virtual path isn't allowed. Any thoughts?

System.Web is already imported.

Eustoliaeutectic answered 27/7, 2009 at 19:39 Comment(0)
S
317

The ServerUtility class is available as an instance in your HttpContext. If you're in an environment where you know it'll be executed inside the ASP.Net pipeline, you can use

HttpContext.Current.Server.MapPath()

You'll have to import System.Web though.

Starchy answered 27/7, 2009 at 19:41 Comment(4)
Even though this post is more than 2 years old, you've helped me tremendously. Thanks.Agrology
What if it is not executed in that pipeline?Deign
If you're not executing inside of ASP.Net then it's unlikely that your HttpContext is being set, unless you wrote your own pipeline :) You'll have to rely on whatever methods your execution context (router?) provides. If your process has insight to the basics of your path routing you can take a look at the System.IO.Path methods.Starchy
This compiled but context is null outside of controllers, so my code errors. I think womp is saying the same thing there. If thats the case can you access MapPath outside of the routers?Woodland
H
35

you can also use:

var path = System.Web.Hosting.HostingEnvironment.MapPath("~/App_Data/myfile.txt")

if

var path = Server.MapPath("~/App_Data");
var fullpath = Path.Combine(path , "myfile.txt");

is inaccessible

Helladic answered 2/3, 2014 at 14:47 Comment(0)
W
10

Can't you just add a reference to System.Web and then you can use Server.MapPath ?

Edit: Nowadays I'd recommend using the HostingEnvironment.MapPath Method:

It's a static method in System.Web assembly that Maps a virtual path to a physical path on the server. It doesn't require a reference to HttpContext.

Weber answered 27/7, 2009 at 19:41 Comment(1)
Sure you can add the reference to an external class; but obviously you need to use it in the context of a server request so HttpContext is not null.Weber
E
4
System.Reflection.Assembly.GetAssembly(type).Location

IF the file you are trying to get is the assembly location for a type. But if the files are relative to the assembly location then you can use this with System.IO namespace to get the exact path of the file.

Elizaelizabet answered 27/7, 2009 at 19:45 Comment(3)
-1: What led you to believe he wanted the location of an assembly?Howlyn
He said "certain files" he didn't specify the location or nature of the files, thus knowing the assembly location and being able to work relative to that path can be helpful. Of course if he actually stated he was still in a HttpContext then I wouldn't have bothered answering.Elizaelizabet
David McEwing, what you suggest won't work because IIS doesn't load the assemblies from the location that you install them in, in the web site. They are copied and loaded from a temporary asp.net cache location, so doing a GetAssembly or GetExecutingAssembly will point you to the location of the assembly, but it would have nothing to do with the location of the web site that MapPath points to.Orrin
I
4

I use this too:

System.Web.HTTPContext.Current.Server.MapPath
Interval answered 27/7, 2011 at 17:1 Comment(0)
S
3
class test
{
public static void useServerPath(string path)
{
   if (File.Exists(path)
{
 \\...... do whatever you wabt
}
else
{
\\.....
}
}

Now when you call the method from the codebehind

for example :

protected void BtAtualizacao_Click(object sender, EventArgs e)
        {
             string path = Server.MapPath("Folder") + "\\anifile.txt";

            test.useServerPath(path);
}

in this way your code is to simple and with one method u can use multiple path for each call :)

Sian answered 14/4, 2011 at 17:46 Comment(0)
P
3

This one helped for me

//System.Web.HttpContext.Current.Server.MapPath //        
FileStream fileStream = new FileStream(System.Web.HttpContext.Current.Server.MapPath("~/File.txt"),
FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite);
Plessor answered 26/12, 2014 at 8:30 Comment(1)
Welcome to stackoverflow. Little more explanation would help out the fellow programmers to understand why the solution worked out.Dawna
A
2

Whether you're running within the context of ASP.NET or not, you should be able to use HostingEnvironment.ApplicationPhysicalPath

Armillas answered 30/7, 2015 at 21:51 Comment(0)
T
0

The server.mappath("") will work on aspx page,if you want to get the absolute path from a class file you have to use this-

HttpContext.Current.Server.MapPath("~/EmailLogic/RegistrationTemplate.html")
Turro answered 26/3, 2018 at 18:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.