Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link
Owner Author

@Jignesh-Darji Jignesh-Darji commented Mar 28, 2018

Remove oldBindingValue

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