Using Server.MapPath in external C# Classes in ASP.NET
Solution 1
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.
Solution 2
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
Solution 3
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
.
Solution 4
I use this too:
System.Web.HTTPContext.Current.Server.MapPath
Solution 5
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.
Chet
I'm a data software engineer at Simon Data in NYC. I have an M.Eng from Cornell University where I studied distributed computing, information storage, retrieval, natural language processing, and some machine learning. Favorite language is Clojure because functional programming with immutable data structures is beautiful when done right, and the real world JVM performance is impressive. In the past I've done a lot of work engineering in web applications on both LAMP and .NET stacks. I love Python, done a lot of Java, C#, Ruby, and worked in PHP. My dev environment is usually OSX, git, and some combination of IntelliJ (Java), Sublime (JS), Emacs (for lisp/Clojure), and Vim (everything else).
Updated on March 27, 2020Comments
-
Chet about 4 years
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 triedHostingEnvironment.MapPath()
, but that complains that the relative virtual path isn't allowed. Any thoughts?System.Web
is already imported. -
John Saunders almost 15 years-1: What led you to believe he wanted the location of an assembly?
-
David McEwing almost 15 yearsHe 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.
-
Dan Diplo almost 14 yearsSure 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.
-
iarwain01 over 12 yearsEven though this post is more than 2 years old, you've helped me tremendously. Thanks.
-
zaitsman about 11 yearsWhat if it is not executed in that pipeline?
-
zumalifeguard almost 10 yearsDavid 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.
-
Nagama Inamdar over 9 yearsWelcome to stackoverflow. Little more explanation would help out the fellow programmers to understand why the solution worked out.
-
nuiun about 8 yearsIf 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.
-
edencorbin about 5 yearsThis 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?