i'm searching a way to get site state of my iis site from an asp.net application.
i've tried to get State from ServerManager Object but it doesn't work
string ConnessioneDB = "server=127.0.0.1;user=userdb;database=reter;password=eLs3qp?4; ";
var iisManager = new ServerManager();
MySqlConnection conn = new MySqlConnection(ConnessioneDB);
conn.Open();
MySqlCommand truncate = conn.CreateCommand();
truncate.CommandText = "truncate table hosting_win";
truncate.ExecuteNonQuery();
foreach (Site site in iisManager.Sites)
{
var hosting = site.Name;
var stato = site.State;
if (hosting != "Default FTP Site" && hosting != "Default Web Site" && hosting != "GetIIS")
{
var server_address = new System.Net.WebClient().DownloadString("http://icanhazip.com");
var binding = GetSiteBindings(site);
string sql_bindings = "";
if (binding != null)
{
foreach (var sitebinding in binding)
{
if (sitebinding != null)
{
var bindingInformation = sitebinding.BindingInformation;
string result = bindingInformation.Substring(bindingInformation.LastIndexOf(":") + 1);
sql_bindings += result + ",";
}
}
MySqlCommand insert = conn.CreateCommand();
insert.CommandText = "insert into hosting_win (hosting,server_address,binding,stato) values (@hosting,@server_address,@binding,@stato)";
insert.Parameters.AddWithValue("@hosting", hosting);
insert.Parameters.AddWithValue("@server_address", server_address);
insert.Parameters.AddWithValue("@binding", sql_bindings);
insert.Parameters.AddWithValue("@stato", stato);
insert.ExecuteNonQuery();
}
}
}
conn.Close();
why i can't get site State?
As far as I know, if your websites contain the FTP sites, it will get this error. Since the ftp sites doesn't cotain the application pool.
I suggest you could try to check the site is ftp site firstly, then you could get the site state.
Details about my test demo, you could refer to below codes:
using Microsoft.Web.Administration;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace IISgetStatus
{
public partial class Status : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
var iisManager = new ServerManager();
string sitedisplay = null;
foreach (Site site in iisManager.Sites)
{
var hosting = site.Name;
string bindingdisplay = null;
if (hosting != "ftpsite")
{
var stato = site.State;
foreach (Binding binding in site.Bindings)
{
bindingdisplay = bindingdisplay + " Binding:\n BindingInformation: " +
binding.BindingInformation;
if (binding.Protocol == "https")
{
// There is a CertificateHash and
// CertificateStoreName for the https protocol only.
bindingdisplay = bindingdisplay + "\n CertificateHash: " +
binding.CertificateHash + ": ";
// Display the hash.
foreach (System.Byte certhashbyte in binding.CertificateHash)
{
bindingdisplay = bindingdisplay + certhashbyte.ToString() + " ";
}
bindingdisplay = bindingdisplay + "\n CertificateStoreName: " +
binding.CertificateStoreName;
}
bindingdisplay = bindingdisplay + "\n EndPoint: " + binding.EndPoint;
bindingdisplay = bindingdisplay + "\n Host: " + binding.Host;
bindingdisplay = bindingdisplay + "\n IsIPPortHostBinding: " + binding.IsIPPortHostBinding;
bindingdisplay = bindingdisplay + "\n Protocol: " + binding.Protocol;
bindingdisplay = bindingdisplay + "\n ToString: " + binding.ToString();
bindingdisplay = bindingdisplay + "\n UseDsMapper: " + binding.UseDsMapper + "\n\n";
}
}
sitedisplay = sitedisplay + bindingdisplay + "\n";
}
Label1.Text = sitedisplay;
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.