Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Path Location to Remote IIS

Tags:

c#

iis

I'm having a unique problem with Internet Information System.

I have several hosted web-sites. Each site contains an Application Pool and Web Site. However when they leave their paid status and become expired. I've designed a service to modify the web-sites physical path.

An example:

private static void ExpireSite(string url)
{
     using(ServerManaged manager = new ServerManager())
     {
          Site expire = manager.Sites[url];
          expire.Applications[url].Path = @"C:\inetpub\wwwroot\Expired";
          manager.CommitChanges();
     }
}

It doesn't modify the path, but I do manage to get an Invalid Path Error.

The url parameter matches the web-site name and directory within the root IIS Folder.

I've followed the Microsoft Developer Network, but unfortunately the article doesn't outline this process very well.

Does anyone know if this is the proper way to find the Physical Path? Is this MSDN article even accurate?

I'd like to add that the documented way, doesn't work:

private static void ExpireSite(string url)
{
     using(ServerManaged manager = new ServerManager())
     {
          Site expire = manager.Sites[url];
          expire.Applications[url].VirtualDirectories["/"].PhysicalPath = @"C:\inetpub\wwwroot\Expired";
          manager.CommitChanges();
     }
}

I receive the same error, both ways.


Update:

For the sake of argument I ran some additional test; I've got around three hundred web-sites. When I run:

using(ServerManager manager = new ServerManager())
{
     Site domain = manager.Sites[url];
     foreach(Application app in expire.Applications)
     {
         Console.WriteLine(domain.Id);
         Console.WriteLine(domain.Name); 
         Console.WriteLine(app.Path);
      }
}

It retrieves their data, but they all have the same path of /. That doesn't seem correct; so if I use this:

domain.Applications[@"/"].VirtualDirectories[@"/"].PhysicalPath = @"C:\inetpub\wwwroot\Expired";

It does indeed change the path, but should they all contain the / path?

like image 742
Greg Avatar asked Nov 29 '25 20:11

Greg


1 Answers

New Answer

First, I still stand by the statement that the Application.Path information isn't being used correctly. Path is meant to be a virtual path not the physical path. Second, I believe that the problem with your original code to get the target application is a simple problem. Namely, that the string you should be using is "/" + url rather than just the url.

To demonstrate this, I set up a new site on my local IIS with an application using the same name as the site

IIS Setup

The physical path of the application under the site can be seen here:

Physical Path Original

I then put together a method to get the target site "mylocalsite.hereiam.com", the app under it with the same name and then attempt to update the path using both the app's Path property as well as the default virtual directory of the app's physical path.

    public static void SetPathTest()
    {
        using (var manager = new ServerManager())
        {
            const string Url = "mylocalsite.hereiam.com";

            var targsite = manager.Sites[Url];

            var targapp = targsite.Applications["/" + Url];
            var worked = false;

            Console.WriteLine("App path: {0}", targapp.Path);

            try
            {
                targapp.Path = @"C:\Projects\Demos\OData\Station.ODataNew";
                Console.WriteLine("Setting Path using Path worked!");
                worked = true;
            }
            catch (Exception)
            {
                Console.WriteLine("Oops. Path isn't settable this way");
            }

            if (!worked)
            {
                try
                {
                    targapp.VirtualDirectories["/"].PhysicalPath = @"C:\Projects\Demos\OData\Station.ODataNew";
                    Console.WriteLine("Setting path using the Virtual directory worked!");
                }
                catch (Exception)
                {
                    Console.WriteLine("Oops. Virtual directory PhysicalPath isn't settable this way");
                }
            }

            manager.CommitChanges();
        }
    }

The result of running this is:

Result

And the physical path was changed:

Physical Path Changed

Hopefully, this is more helpful than the first go around.

Old Answer

I think that based on the documentation in the documentation for Application.Path, the value you're putting into the property should be "/Expired" instead of "C:\inetpub\wwwroot\Expired". If you want to use the full path, PhysicalPath of the VirtualDirectories property is the correct property to set.

like image 84
Scott Corbett Avatar answered Dec 02 '25 15:12

Scott Corbett



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!