Cannot use Server.MapPath

前端 未结 8 1424
悲哀的现实
悲哀的现实 2020-12-02 10:11

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

what else? When I type Server there is no quick resu

相关标签:
8条回答
  • 2020-12-02 10:44

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

    0 讨论(0)
  • 2020-12-02 10:51

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

    HttpContext.Current.Server.MapPath(path);
    
    0 讨论(0)
  • 2020-12-02 10:53

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

    0 讨论(0)
  • 2020-12-02 10:54
    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);
    
    0 讨论(0)
  • 2020-12-02 11:01

    you can try using this

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

    or use HostingEnvironment.MapPath

        System.Web.Hosting.HostingEnvironment.MapPath(path);
    
    0 讨论(0)
  • 2020-12-02 11:04

    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.

    0 讨论(0)
提交回复
热议问题