question

IonutDragomir-0256 avatar image
0 Votes"
IonutDragomir-0256 asked SamWu-MSFT commented

How to get website's physical path on IIS 10?


In my MVC app I want to get website's physical path on IIS server. My website's properties:

203651-image.png


What I have tried is the following:

 System.Diagnostics.Debug.WriteLine("MapPath: " + System.Web.Hosting.HostingEnvironment.MapPath("~/server/publish/"));

The above line is returning the path where my project was created:

MapPath: C:\ProjectVisualStudio\Reporting\CS\ServerSide\server\publish\

So, to get the physical path on IIS server, I was trying to obtain it with:

 int iisNumber = 2;
    
             using (ServerManager serverManager = new ServerManager())
             {
                 var site = serverManager.Sites.Where(s => s.Id == iisNumber).SingleOrDefault();
                 var applicationRoot = site.Applications.Where(a => a.Path == "~/server/publish/").SingleOrDefault(); //here I get the exception
             //    var applicationRoot = site.Applications.SingleOrDefault();
                 var virtualRoot = applicationRoot.VirtualDirectories.Where(v => v.Path == "~/server/publish/").SingleOrDefault();
                 Console.WriteLine(virtualRoot.PhysicalPath);
                 System.Diagnostics.Debug.WriteLine("***************************************path IIS: " + virtualRoot.PhysicalPath + "***************************************");               
             }

But I get an exception

System.NullReferenceException: 'Object reference not set to an instance of an object.' site was null.

As you can see in the image, my site has the id = 2. Although running this:

         System.Diagnostics.Debug.WriteLine($"Available sites: {string.Join(", ", serverManager.Sites.Select(s => s.Id))}");

is returnning:

Available sites: 1
Available sites: 1

What could I do to accomplish this task?











dotnet-csharpwindows-server-iisdotnet-aspnet-mvc
image.png (38.3 KiB)
· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.


What happens in case of 'int iisNumber = 1'?


0 Votes 0 ·

Hello, Viorel!

If int iisNumber = 1, I get this: System.NullReferenceException: 'Object reference not set to an instance of an object.'applicationRoot was null at this line:
var virtualRoot = applicationRoot.VirtualDirectories.Where(v => v.Path == "~/server/publish/").SingleOrDefault();

0 Votes 0 ·
Viorel-1 avatar image Viorel-1 IonutDragomir-0256 ·

Did you investigate which paths are available in site.Applications?


0 Votes 0 ·
Bruce-SqlWork avatar image
0 Votes"
Bruce-SqlWork answered Bruce-SqlWork commented

if you run your application with IIS, instead of IIS Express, the initial code is correct.

System.Diagnostics.Debug.WriteLine("MapPath: " + System.Web.Hosting.HostingEnvironment.MapPath("~/server/publish/"));

will return the IIS virtual directory path, not the IIS express one (which is mapped to the source).

· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

I don't have IIS Express. The screenshot I shared is from my IIS. So if I'd run the site from IIS, I'd get the path C:\ReportDesigner\server\publish?

0 Votes 0 ·
Bruce-SqlWork avatar image Bruce-SqlWork IonutDragomir-0256 ·

its depends on what the physical path in IIS is for the application

0 Votes 0 ·
SamWu-MSFT avatar image
0 Votes"
SamWu-MSFT answered SamWu-MSFT commented

@IonutDragomir-0256

a => a.Path == "~/server/publish/"

Maybe there is something wrong with your path, you can try the following code:

 ServerManager serverManager = new ServerManager();  

 // get the site (e.g. default)
 Site site = serverManager.Sites.FirstOrDefault(s => s.Name == "Default Web Site");

 // get the application that you are interested in
 Application myApp = site.Applications["/Dev1"];

 // get the physical path of the virtual directory
 Console.WriteLine(myApp.VirtualDirectories[0].PhysicalPath);

If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

I'm still getting the exception on

Application myApp = site.Applications["/Dev1"];

My code using your example:



         using (ServerManager serverManager = new ServerManager())
         {
        //     ServerManager serverManager = new ServerManager();
             // get the site (e.g. default)
             Site site = serverManager.Sites.FirstOrDefault(s => s.Name == "Default Web Site");
             // get the application that you are interested in
             Application myApp = site.Applications["~/server/publish/"];
             // get the physical path of the virtual directory
             Console.WriteLine(myApp.VirtualDirectories[0].PhysicalPath);
             System.Diagnostics.Debug.WriteLine("***************************************publish: " + myApp.VirtualDirectories[0].PhysicalPath + "***************************************");
             Console.WriteLine("World");
         }

Why do I get this exception when debugging the code?

0 Votes 0 ·
SamWu-MSFT avatar image SamWu-MSFT IonutDragomir-0256 ·

@IonutDragomir-0256

All applications need to have their paths starting with /, so you should use /server/publish/.

0 Votes 0 ·