Cannot use Server.MapPath

C#Visual StudioIntellisenseserver.mappath

C# Problem Overview


What I must do to make Server.MapPath work?
I have using System.Web;

what else? When I type Server there is no quick result option (intelli-sense) for Server.

Any help?

C# Solutions


Solution 1 - C#

you can try using this

    System.Web.HttpContext.Current.Server.MapPath(path);

or use HostingEnvironment.MapPath

    System.Web.Hosting.HostingEnvironment.MapPath(path);

Solution 2 - C#

Your project needs to reference assembly System.Web.dll. Server is an object of type HttpServerUtility. Example:

HttpContext.Current.Server.MapPath(path);

Solution 3 - C#

System.Web.HttpContext.Current.Server.MapPath("~/") gives null if we call it from a thread.

So, Try to use

System.Web.Hosting.HostingEnvironment.MapPath("~/")

Solution 4 - C#

Firt add a reference to System.web, if you don't have. Do that in the References folder.

You can then use Hosting.HostingEnvironment.MapPath(path);

Solution 5 - C#

bool IsExist = System.IO.Directory.Exists(HttpContext.Current.Server.MapPath("/UploadedFiles/"));
if (!IsExist)
    System.IO.Directory.CreateDirectory(HttpContext.Current.Server.MapPath("/UploadedFiles/"));
            
StreamWriter textWriter = File.CreateText(Path.Combine(HttpContext.Current.Server.MapPath("/UploadedFiles/") + "FileName.csv"));
var csvWriter = new CsvWriter(textWriter, System.Globalization.CultureInfo.CurrentCulture);
csvWriter.WriteRecords(classVM);

Solution 6 - C#

Try adding System.Web as a reference to your project.

Solution 7 - C#

You need to add reference (System.Web) Reference to System.Web

Solution 8 - C#

I know this post is a few years old, but what I do is add this line to the top of your class and you will still be able to user Server.MapPath

Dim Server = HttpContext.Current.Server

or u can make a function

Public Function MapPath(sPath as String)
    return HttpContext.Current.Server.MapPath(sPath)
End Function

I am all about making things easier. I have also added it to my Utilities class just in case i run into this again.

Solution 9 - C#

I faced the same problem and I guess this might help someone. As the Original Poster of this question asked

> What I must do to make Server.MapPath work?
> I have using System.Web;

The class which we had written should implement System.Web.UI.Page

Say for example, our classname is MyClass

public class MyClass: System.Web.UI.Page
{
// Code runs here 
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questiona1204773View Question on Stackoverflow
Solution 1 - C#DotNetUserView Answer on Stackoverflow
Solution 2 - C#Leandro GomideView Answer on Stackoverflow
Solution 3 - C#Ravindra Singh ChhabraView Answer on Stackoverflow
Solution 4 - C#jdislaView Answer on Stackoverflow
Solution 5 - C#user13048334View Answer on Stackoverflow
Solution 6 - C#jabu.hlongView Answer on Stackoverflow
Solution 7 - C#Aaditya DubeyView Answer on Stackoverflow
Solution 8 - C#Guy CothalView Answer on Stackoverflow
Solution 9 - C#Loves2CodeView Answer on Stackoverflow