Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Jignesh-Darji/482eebf17e0cb61a93dbadc34e3a7ea8 to your computer and use it in GitHub Desktop.
Save Jignesh-Darji/482eebf17e0cb61a93dbadc34e3a7ea8 to your computer and use it in GitHub Desktop.
How to change bindings on a site in IIS using C#
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!");
}
}
}
}
@Jignesh-Darji
Copy link
Author

Remove oldBindingValue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment