【菜科解读】
这里说的不是如何解决路径重写或者如何配置的问题,而是阐述一下站点(site),应用程序(application)和虚拟目录(virtual directory)概念与作用,已及这三个东西在IIS6与IIS7中的异同。
因为站点,应用程序和虚拟目录是我们在IIS上架设网站时肯定会遇到的,但它们的概念又是那么的含糊(至少对我来说)。
我在网上找了一些资料来学习,其中一个写的比较详细的是:Understanding Sites, Applications, and Virtual Directories on IIS 7而这里则只是作一些总结。

在IIS6中,应用程序和虚拟目录的概率是有点含糊的,而在IIS7以上,这三者则被规范化起来,在IIS架构层面上明确了三者的层次关系。
在IIS6中,虽然存在virtual directory 和application两个概念,而且看上去这两个概念是独立的,但是在IIS6中一个application其实就是一个虚拟目录,只不过一个application在medabase中可以对AppFriendlyName, AppRoot, AppIsolated, AppPoolID这几个属性进行设置而已。
但IIS6中的site则是例外,因为就算不对那几个属性进行设置,它也不会被视为虚拟目录,而是被视为一个application。
而在IIS7或以上(但目前我能接触到的最高版本的IIS就是win7中的IIS7.5),site,application和virtual directory的概念已经被规范起来,已经不像IIS6那样含糊。
在IIS7+中,他们是独立的概念,并且在IIS组织架构上呈现出一种层次关系:一个site中可以有一个或者多个application,一个application中可以有一个或者多个virtual directory,而一个virtual directory则对应着一个物理路径。
一个site默认会至少有一个application,称为根应用程序(root application)或者默认应用程序(default application),而一个application至少有一个vitual director,称为根虚拟目录(root virtual directory)来看一下我的IIS7.5上一个site的结构和这个site在IIS的ApplicationHost.config文件是怎样对应的。
:

注:ApplicationHost.config文件在目录:\%windir%\system\inetsrv\config目录下
我的IIS中只有一个ID为13的site,site下有两个application分别为cd和dh。
从右边的config中可以看到,其实除了cd和dh两个application外,site本身也是一个application,也就是root application。
同时也可以看到,每个application下有一个 virtual directory,也就是root virtual directory,充当着这个application的根目录,并映射到该application所在的物理路径。
当然,每个application可以有多个virtual directory,这些virtual directory可以对应其他的物理路径(譬如dh application下的image虚拟目录的物理路径可以使网络中另外一台计算机的某个共享目录)
在IIS7+中(其实IIS6也是一样,但细节有不同,这里有点含糊,还待深入研究),一个site运行在一个application pool中,而一个application pool默认有一个w3wp.exe(工作者进程),site中的application运行在这个w3wp.exe进程中的app domain(应用程序域)中(不同application运行在不同app domain中,以进行隔离),而同一个application的virtual directory运行在相同的app domain下。
但site下的application不一定必须跟这个site运行在相同的application pool,application可以指定一个跟这个application的site不同的application pool。
接下来独立的阐述一下site,application和virtual derectory
站点(site)

一个站点包含一个或者多个application和一个或者多个虚拟目录。
我们可以通过对site进行不同的绑定以用不同的方式对site进行访问。
这里的“绑定”包含两个方面,一个是绑定的协议,另一个就是绑定信息。
绑定协议用于指定通过什么协议去和该site进行通信。
IIS7+中,对一个site可以的协议包括http,https,net.tcp,net.pipe,net.msmq,net.formatname这几种。
当然,对于一个网站,最常用的就是http和https。
而绑定信息则定义了通信的基本信息,比如IP地址,通信端口,站点的一些头部信息(header)。
正如前面说到的,可以为一个site添加多种绑定,只需要在IIS中对某个site进行“编辑绑定”操作即可。
应用程序(application)
application是为一个site提供功能的基本单位,例如一个购物站点可以包含两个application:一个负责呈现商品,给消费者去选购,并放入购物车,而另一个appliation则可以专注于用户的登录以及支付业务。
当一个site只有一个application时候,这个application也就是root application或者default application,代表着这个site本身,在applicationHost中的“ 下一篇:
Javascript中的“作用域链”知识分享
7 for(var i = 0; i发生了什么事情很多人都可能知道上例的执行结果,但是不是所有人都明白为什么会是这样的结果,包括我自己。
名词解释活动对象:一次函数调用开始的时候,javascript解释器会收集函数体中的所有局部变量(以var形式声明的变量),将这些局部变量存储到一个称为“活动对象”的对象里,所有变量都初始为undefined。
代码示例1 var fun = function(){2 alert(name);3 var name = ‘段光伟‘;4 }当执行这个函数时候时(fun()),函数体还没执行到,当前的活动对象为[{ name: undefined }],因此fun()执行的结果为:函数的[scope]属性:每个函数在定义的时候(生成函数实例的时候)都会分配一个[scope]属性,这个属性指向的当前的“作用域链”。
这个属性开发人员是访问不到的,只有javascript能访问。
作用域链:当函数调用时,javascript引擎会维护一个这次调用的作用域链,这个作用域链条是函数的[scope]指向的作用域链加上函数调用时的活动对象,形式如[ 活动对象, 函数定义时的作用域链条]。
代码示例 1 var a = 1; 2 //步骤1:[ { a: 1, outer: undefined } ] 4 var outer = function(){ 5 //步骤3:[ { b: undefined, inner: undefined } ,{ a: 1, outer: function } ] 6 var b = 2; 7 var inner = function(){ 8 //步骤4:[ {}, { b: 2, inner: function } ,{ a: 1, outer: function } ] 9 return a + b;12 //步骤3:[ { b: 2, inner: function } ,{ a: 1, outer: function } ]13 return inner();16 //步骤2:[ { a: 1, outer: function } ]17 outer();作用域链规则规则1javascript一般运行在一定的宿主中,每个宿主都会提供一个“全局对象”,或者叫“全局活动对象”,这个全局对象是所有作用域链的根节点。
规则2“取值操作”(如:alert(xxxVar))的规则是,沿着作用域链依次查找名称为“xxxVar”的变量,返回第一个找到的值,如果找不到就抛出异常(ReferenceError: xxxVar is not defined)。
规则3“赋值操作”(如:xxxVar = ‘段光伟‘)的规则是,沿着作用域链依次查找名称为“xxxVar”的变量,覆盖第一个找到的值,如果找不到就将“xxxVar”添加到全局对象中。
备注“闭包”这个概念就是通过“作用域链”实现的,而C#是通过编译器实现的,.NET并不支持。
Javascript,中的,“,作用域链,”,知识,分享,代
C#操作IIS7程序池新建站点站点配置
最近在做一个WEB程序的安装包;对一些操作IIS进行一个简单的总结;主要包括对IIS进行站点的新建以及新建站点的NET版本的选择,还有针对IIS7程序池的托管模式以及版本的操作;首先要对Microsoft.Web.Administration进行引用,它主要是用来操作IIS7;using System.DirectoryServices;using Microsoft.Web.Administration;1:首先是对本版IIS的版本进行配置:DirectoryEntry getEntity = new DirectoryEntry("IIS://localhost/W3SVC/INFO"); string Version = getEntity.Properties["MajorIISVersionNumber"].Value.ToString(); MessageBox.Show("IIS版本为:" + Version);2:是判断程序池是存在; /// /// 判断程序池是否存在 /// /// 程序池名称 /// true存在 false不存在 private bool IsAppPoolName(string AppPoolName) bool result = false; DirectoryEntry appPools = new DirectoryEntry("IIS://localhost/W3SVC/AppPools"); foreach (DirectoryEntry getdir in appPools.Children) if (getdir.Name.Equals(AppPoolName)) result = true; return result; }3:删除应用程序池 /// /// 删除指定程序池 /// /// 程序池名称 /// true删除成功 false删除失败 private bool DeleteAppPool(string AppPoolName) bool result = false; DirectoryEntry appPools = new DirectoryEntry("IIS://localhost/W3SVC/AppPools"); foreach (DirectoryEntry getdir in appPools.Children) if (getdir.Name.Equals(AppPoolName)) getdir.DeleteTree(); result = true; catch result = false; return result; }4:创建应用程序池 (对程序池的设置主要是针对IIS7;IIS7应用程序池托管模式主要包括集成跟经典模式,并进行NET版本的设置) string AppPoolName = "LamAppPool"; if (!IsAppPoolName(AppPoolName)) DirectoryEntry newpool; DirectoryEntry appPools = new DirectoryEntry("IIS://localhost/W3SVC/AppPools"); newpool = appPools.Children.Add(AppPoolName, "IIsApplicationPool"); newpool.CommitChanges(); MessageBox.Show(AppPoolName + "程序池增加成功"); #endregion #region 修改应用程序的配置(包含托管模式及其NET运行版本) ServerManager sm = new ServerManager(); sm.ApplicationPools[AppPoolName].ManagedRuntimeVersion = "v4.0"; sm.ApplicationPools[AppPoolName].ManagedPipelineMode = ManagedPipelineMode.Classic; //托管模式Integrated为集成 Classic为经典 sm.CommitChanges(); MessageBox.Show(AppPoolName + "程序池托管管道模式:" + sm.ApplicationPools[AppPoolName].ManagedPipelineMode.ToString() + "运行的NET版本为:" + sm.ApplicationPools[AppPoolName].ManagedRuntimeVersion);运用C#代码来对IIS7程序池托管管道模式及版本进行修改;5:针对IIS6的NET版进行设置;因为此处我是用到NET4.0所以V4.0.30319 若是NET2.0则在这进行修改v2.0.50727 //启动aspnet_regiis.exe程序 string fileName = Environment.GetEnvironmentVariable("windir") + @"\Microsoft.NET\Framework\v4.0.30319\aspnet_regiis.exe"; ProcessStartInfo startInfo = new ProcessStartInfo(fileName); //处理目录路径 string path = vdEntry.Path.ToUpper(); int index = path.IndexOf("W3SVC"); path = path.Remove(0, index); //启动ASPnet_iis.exe程序,刷新脚本映射 startInfo.Arguments = "-s " + path; startInfo.WindowStyle = ProcessWindowStyle.Hidden; startInfo.UseShellExecute = false; startInfo.CreateNoWindow = true; startInfo.RedirectStandardOutput = true; startInfo.RedirectStandardError = true; Process process = new Process(); process.StartInfo = startInfo; process.Start(); process.WaitForExit(); string errors = process.StandardError.ReadToEnd();6:平常我们可能还得对IIS中的MIME类型进行增加;下面主要是我们用到两个类型分别是:xaml,xap IISOle.MimeMapClass NewMime = new IISOle.MimeMapClass(); NewMime.Extension = ".xaml"; NewMime.MimeType = "application/xaml+xml"; IISOle.MimeMapClass TwoMime = new IISOle.MimeMapClass(); TwoMime.Extension = ".xap"; TwoMime.MimeType = "application/x-silverlight-app"; rootEntry.Properties["MimeMap"].Add(NewMime); rootEntry.Properties["MimeMap"].Add(TwoMime); rootEntry.CommitChanges();7:下面是做安装时一段对IIS进行操作的代码;兼容IIS6及IIS7;新建虚拟目录并对相应的属性进行设置;对IIS7还进行新建程序池的程序;并设置程序池的配置;/// /// 创建网站 /// /// public void CreateNewWebSite(NewWebSiteInfo siteInfo) if (!EnsureNewSiteEnavaible(siteInfo.BindString)) throw new Exception("该网站已存在" + Environment.NewLine + siteInfo.BindString); DirectoryEntry rootEntry = GetDirectoryEntry(entPath); newSiteNum = GetNewWebSiteID(); DirectoryEntry newSiteEntry = rootEntry.Children.Add(newSiteNum, "IIsWebServer"); newSiteEntry.CommitChanges(); newSiteEntry.Properties["ServerBindings"].Value = siteInfo.BindString; newSiteEntry.Properties["ServerComment"].Value = siteInfo.CommentOfWebSite; newSiteEntry.CommitChanges(); DirectoryEntry vdEntry = newSiteEntry.Children.Add("root", "IIsWebVirtualDir"); vdEntry.CommitChanges(); string ChangWebPath = siteInfo.WebPath.Trim().Remove(siteInfo.WebPath.Trim().LastIndexOf(‘\\‘),1); vdEntry.Properties["Path"].Value = ChangWebPath; vdEntry.Properties["AccessRead"][0] = true; //设置读取权限 vdEntry.Properties["AccessWrite"][0] = true; vdEntry.Properties["AccessScript"][0] = true;//执行权限 vdEntry.Properties["AccessExecute"][0] = false; vdEntry.Properties["DefaultDoc"][0] = "Login.aspx";//设置默认文档 vdEntry.Properties["AppFriendlyName"][0] = "LabManager"; //应用程序名称 vdEntry.Properties["AuthFlags"][0] = 1;//0表示不允许匿名访问,1表示就可以3为基本身份验证,7为windows继承身份验证 vdEntry.CommitChanges(); //操作增加MIME //IISOle.MimeMapClass NewMime = new IISOle.MimeMapClass(); //NewMime.Extension = ".xaml"; NewMime.MimeType = "application/xaml+xml"; //IISOle.MimeMapClass TwoMime = new IISOle.MimeMapClass(); //TwoMime.Extension = ".xap"; TwoMime.MimeType = "application/x-silverlight-app"; //rootEntry.Properties["MimeMap"].Add(NewMime); //rootEntry.Properties["MimeMap"].Add(TwoMime); //rootEntry.CommitChanges(); #region 针对IIS7 DirectoryEntry getEntity = new DirectoryEntry("IIS://localhost/W3SVC/INFO"); int Version =int.Parse(getEntity.Properties["MajorIISVersionNumber"].Value.ToString()); if (Version > 6) #region 创建应用程序池 string AppPoolName = "LabManager"; if (!IsAppPoolName(AppPoolName)) DirectoryEntry newpool; DirectoryEntry appPools = new DirectoryEntry("IIS://localhost/W3SVC/AppPools"); newpool = appPools.Children.Add(AppPoolName, "IIsApplicationPool"); newpool.CommitChanges(); #endregion #region 修改应用程序的配置(包含托管模式及其NET运行版本) ServerManager sm = new ServerManager(); sm.ApplicationPools[AppPoolName].ManagedRuntimeVersion = "v4.0"; sm.ApplicationPools[AppPoolName].ManagedPipelineMode = ManagedPipelineMode.Classic; //托管模式Integrated为集成 Classic为经典 sm.CommitChanges(); #endregion vdEntry.Properties["AppPoolId"].Value = AppPoolName; vdEntry.CommitChanges(); #endregion //启动aspnet_regiis.exe程序 string fileName = Environment.GetEnvironmentVariable("windir") + @"\Microsoft.NET\Framework\v4.0.30319\aspnet_regiis.exe"; ProcessStartInfo startInfo = new ProcessStartInfo(fileName); //处理目录路径 string path = vdEntry.Path.ToUpper(); int index = path.IndexOf("W3SVC"); path = path.Remove(0, index); //启动ASPnet_iis.exe程序,刷新脚本映射 startInfo.Arguments = "-s " + path; startInfo.WindowStyle = ProcessWindowStyle.Hidden; startInfo.UseShellExecute = false; startInfo.CreateNoWindow = true; startInfo.RedirectStandardOutput = true; startInfo.RedirectStandardError = true; Process process = new Process(); process.StartInfo = startInfo; process.Start(); process.WaitForExit(); string errors = process.StandardError.ReadToEnd(); if (errors != string.Empty) throw new Exception(errors); }string entPath = String.Format("IIS://{0}/w3svc", "localhost");public DirectoryEntry GetDirectoryEntry(string entPath) DirectoryEntry ent = new DirectoryEntry(entPath); return ent; public class NewWebSiteInfo private string hostIP; // 主机IP private string portNum; // 网站端口号 private string descOfWebSite; // 网站表示。
一般为网站的网站名。
例如"www.dns.com.cn" private string commentOfWebSite;// 网站注释。
一般也为网站的网站名。
private string webPath; // 网站的主目录。
例如"e:\ mp" public NewWebSiteInfo(string hostIP, string portNum, string descOfWebSite, string commentOfWebSite, string webPath) this.hostIP = hostIP; this.portNum = portNum; this.descOfWebSite = descOfWebSite; this.commentOfWebSite = commentOfWebSite; this.webPath = webPath; public string BindString return String.Format("{0}:{1}:{2}", hostIP, portNum, descOfWebSite); //网站标识(IP,端口,主机头值) public string PortNum return portNum; public string CommentOfWebSite return commentOfWebSite; public string WebPath return webPath; }8:下面的代码是对文件夹权限进行设置,下面代码是创建Everyone 并给予全部权限 /// /// 设置文件夹权限 处理给EVERONE赋予所有权限 /// /// 文件夹路径 public void SetFileRole() string FileAdd = this.Context.Parameters["installdir"].ToString(); FileAdd = FileAdd.Remove(FileAdd.LastIndexOf(‘\\‘), 1); DirectorySecurity fSec = new DirectorySecurity(); fSec.AddAccessRule(new FileSystemAccessRule("Everyone",FileSystemRights.FullControl,InheritanceFlags.ContainerInherit|InheritanceFlags.ObjectInherit,PropagationFlags.None,AccessControlType.Allow)); System.IO.Directory.SetAccessControl(FileAdd, fSec); } 操作,IIS7,程序,池,新建,站点,配置,最近,在做,