0

I am calling a method through ajax. My ajax is working fine.

But it throws compile time error on my code.

Here is my code

[WebMethod]
    public static List<string> getDirectoryNames(string dirLocation)
    {
        string paths = dirLocation;
        List<string> dir = new List<string>();

        //string getPat = getPath("");


        foreach(var dirr in new DirectoryInfo(Server.MapPath(@paths)).GetDirectories())   //error on this **Server**
        {
            dir.Add(dirr.Name);
        }

        return dir;
    }

I think I cannot declare Server.MapPath on Static method. So what I have to do?

  • `Server` is a property of the `Page`. So you need an instance of the current page which is not available in a webmethod. But you can use [`System.Web.Hosting.HostingEnvironment.MapPath`](https://msdn.microsoft.com/en-us/library/system.web.hosting.hostingenvironment.mappath.aspx). – Tim Schmelter Apr 11 '18 at 09:11
  • @TimSchmelter thanks bro. It's working – kevin Peter Apr 11 '18 at 09:28

0 Answers0