Forked from vnisor/gist:42ebcc196ab2d8675df46adfc191242c
Last active
March 28, 2018 10:19
-
-
Save Jignesh-Darji/482eebf17e0cb61a93dbadc34e3a7ea8 to your computer and use it in GitHub Desktop.
How to change bindings on a site in IIS using C#
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void ChangeSiteBinding(string siteName, string newBindingValue, string protocol) | |
{ | |
using (ServerManager manager = new ServerManager()) | |
{ | |
// Find the site by name from IIS | |
Microsoft.Web.Administration.Site site = manager.Sites.Where(q => q.Name == siteName).FirstOrDefault(); | |
if (site == null) | |
{ | |
throw new Exception("The specified site name does not exist in IIS!"); | |
} | |
else | |
{ | |
bool found = false; | |
// Loop through all the site's bindings | |
foreach (var binding in site.Bindings) | |
{ | |
// If the binding protocol matches the passed protocol, continue | |
if (binding.Protocol.Equals(protocol)) | |
{ | |
// Set this to indicate the binding was found | |
found = true; | |
// Set the binding value to the new passed value | |
binding.BindingInformation = newBindingValue; | |
break; | |
} | |
} | |
if (found) | |
{ | |
// If the binding was found and changed, commit the changes. | |
manager.CommitChanges(); | |
} | |
else | |
{ | |
// If the binding was not found, throw an exception indicating as much. | |
throw new Exception("The specified old binding does not exist on this site!"); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Remove oldBindingValue