MVC例子:
DefaultController部分代码如下:
public ActionResult Index()
{
List<IISSiteEntity> list_site=new List<IISSiteEntity>();
ServerManager iisManager = new ServerManager();
foreach (var site in iisManager.Sites)//遍历网站
{
IISSiteEntity s = new IISSiteEntity();
s.Id = site.Id;
s.Name = site.Name;
s.PhysicalPath = site.Applications["/"].VirtualDirectories["/"].PhysicalPath;
s.Pool = site.Applications["/"].ApplicationPoolName;
s.List_url = new List<string>();
try
{
foreach (var tmp in site.Bindings)
{
string url = tmp.Protocol + "://";
if (tmp.EndPoint.Address.ToString() == "0.0.0.0")
{
url += tmp.Host + ":" + tmp.EndPoint.Port;
}
else
{
url += tmp.EndPoint.Address.ToString() + ":" + tmp.EndPoint.Port;
}
s.List_url.Add(url);
}
}
catch (Exception ex)
{
}
list_site.Add(s);
}
string ss = HttpContext.Request.ServerVariables["SERVER_SOFTWARE"];
ViewBag.s = ss;
return View(list_site);
}
/// <summary>
/// 回收应用程序池
/// </summary>
/// <param name="PoolName"></param>
/// <returns></returns>
[HttpPost]
public ActionResult Recycile(string PoolName)
{
try
{
ServerManager iisManager = new ServerManager();
iisManager.ApplicationPools[PoolName].Recycle();
}
catch (Exception ex)
{
return Content(ex.Message);
}
return Content("1");
}
/// <summary>
/// 停止或者启动网站
/// </summary>
/// <param name="SiteName"></param>
/// <returns></returns>
public ActionResult ChangeState(string SiteName)
{
try
{
ServerManager iisManager = new ServerManager();
var site = iisManager.Sites[SiteName];
if (site.State == ObjectState.Started)
{
site.Stop();
}
else if (site.State == ObjectState.Stopped)
{
site.Start();
}
}
catch (Exception ex)
{
return Content(ex.Message);
}
return Content("1");
}
public ActionResult AddSite()
{
return View();
}
[HttpPost]
public ActionResult AddSite(string siteName,string port,string path)
{
//string SiteName = ""; //站点名称
//string BindArgs = "*:89:"; //绑定参数,注意格式
//string apl = "http"; //类型
//string path = "d:\\发布"; //网站路径
ServerManager sm = new ServerManager();
sm.Sites.Add(siteName, "http", port, path);
sm.CommitChanges();
return View();
}
[HttpPost]
public ActionResult DelSite(string siteName)
{
try
{
ServerManager iisManager = new ServerManager();
Site site = iisManager.Sites[siteName];
iisManager.Sites.Remove(site);
iisManager.CommitChanges();
}
catch (Exception ex)
{
return Content(ex.Message);
}
return Content("1");
}
Model的代码如下:
public class IISSiteEntity
{
/// <summary>
/// 网站名称
/// </summary>
public string Name { get; set; }
/// <summary>
/// 网站ID
/// </summary>
public long Id { get; set; }
/// <summary>
/// 物理路径
/// </summary>
public string PhysicalPath { get; set; }
/// <summary>
/// 运行状态
/// </summary>
public string State { get; set; }
/// <summary>
/// 应用程序池
/// </summary>
public string Pool { get; set; }
public List<string> List_url { get; set; }
}
View的代码如下:
@{
ViewBag.Title = "Index";
}
@model List<IISManager.Models.IISSiteEntity>
<h2>IIS 网站管理</h2>
<table>
<thead>
<tr>
<th>ID</th>
<th>名称</th>
<th>物理路径</th>
<th>应用程序池</th>
<th>地址</th>
<th>状态</th>
<th>操作</th>
</tr>
</thead>
<tbody>
@foreach (var m in Model)
{
<tr>
<td>@m.Id</td>
<td>@m.Name</td>
<td>@m.PhysicalPath</td>
<td>@m.Pool</td>
<td>
@foreach (var u in m.List_url)
{
@(u)<br/>
}
</td>
<td>@m.State</td>
<td>
<input type="button" value="回收" οnclick=" recycile('@(m.Pool)') " />
<input type="button" value="关闭" οnclick=" stop('@(m.Name)') " />
<input type="button" value="删除" οnclick=" del('@(m.Name)') " />
</td>
</tr>
}
</tbody>
@ViewBag.s
</table>
@section Foot{
<script>
$(function() {
})
function recycile(name) {
$.ajax({
type: "POST",
url: "/Default/Recycile",
data: "PoolName=" + name,
success: function(msg) {
if (msg == "1") {
location = location;
} else {
alert(msg);
}
}
});
}
function del(name) {
$.ajax({
type: "POST",
url: "/Default/DelSite",
data: "SiteName=" + name,
success: function(msg) {
if (msg == "1") {
location = location;
} else {
alert(msg);
}
}
});
}
function stop(name) {
$.ajax({
type: "POST",
url: "/Default/ChangeState",
data: "SiteName=" + name,
success: function(msg) {
if (msg == "1") {
location = location;
} else {
alert(msg);
}
}
});
}
</script>
}
结果如图: