Web管理IIS8.0 Express

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>
}
结果如图:


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值