安装MVC项目时自动给IIS添加通配符映射

在IIS6中安装ASP.NET MVC项目时,需要将aspnet_isapi.dll添加到网站虚拟目录的通配符映射当中,很多时候我们需要手动完成。

这几天弄了个ASP.NET MVC3的项目,写完后做了一个安装部署程序,想安装的时候能自动将aspnet_isapi.dll添加到虚拟目录的通配符映射当中,于是写了下面的VBS脚本,脚本支持将虚拟目录当作参数传递进来,可以自动给虚拟目录添加通配符映射。可以把这个脚本另存为.vbs文件,然后放到安装类当中使用 System.Diagnostics.Process.Start(vbsFile,virtualPath) 方法进行调用。安装目录可以使用/targetDir=[TARGETDIR]取得,然后处理后获得虚拟目录名。

如何制作安装项目及如何传递参数可以参考:http://kb.cnblogs.com/page/73922/

以下是VBS脚本程序:

 

 1 Option Explicit
 2 Dim virtualPath
 3 Dim msgTitle
 4 msgTitle = "添加脚本映射"
 5 If WScript.Arguments.Length > 0 Then
 6     virtualPath = WScript.Arguments(0)
 7 Else
 8     virtualPath = "Web"
 9 End If
10 
11 Public Function AppMap()
12  Dim oVirtDir
13  set oVirtDir = Nothing
14  On Error Resume Next
16  Set oVirtDir = GetObject("IIS://localhost/W3Svc/1/Root/" & virtualPath)
17  If Err <> 0 Then
18   MsgBox "未能创建 IIS 管理对象!" & vbCrLf & Err.Description,vbOKOnly&vbExclamation,msgTitle
19  End If
20  If Not oVirtDir Is Nothing Then
21   MapHandlers oVirtDir
22  Else
23   MsgBox "添加映射失败!",vbOKOnly&vbExclamation,msgTitle
24  End If
25  On Error GoTo 0
26 End Function
27  
28 Sub MapHandlers(oVirtDir)
29  Err.Number = 0
30  Dim scriptMaps
31  scriptMaps = oVirtDir.GetEx("ScriptMaps")
32  If Err <> 0 Then
33   MsgBox "未能获取当前脚本映射属性。"& vbCrLf & Err.Description,vbOKOnly&vbExclamation,msgTitle
34   Exit Sub
35  End If
36  Dim iMap
37  Dim sourceMap
38  Dim newMap
39  newMap = ""
40  For iMap = LBound(scriptMaps) To UBound(scriptMaps)
41   If Left(scriptMaps(iMap), 6) = ".aspx," Then
42    sourceMap = scriptMaps(iMap)
43    'MsgBox "Found aspx: " & newMap
44   End If
45   If Left(scriptMaps(iMap), 3) = ".*," Then
46    'MsgBox "已经添加了映射"
47     Exit Sub
48   End If
49  Next
50  If sourceMap = "" Then
51   MsgBox "未能找到aspx脚本映射",vbOKOnly&vbExclamation,msgTitle
52   exit sub
53  End If
54  Redim Preserve scriptMaps(UBound(scriptMaps) + 1)
55  newMap = Replace(sourceMap, ".aspx,""*,")
56  scriptMaps(UBound(scriptMaps)) = newMap
57  'MsgBox scriptMaps(UBound(scriptMaps))
58  oVirtDir.PutEx 2"scriptMaps", scriptMaps
59  If Err <> 0 Then
60   MsgBox "保存脚本映射失败!" & vbCrLf & Err.Description,vbOKOnly&vbExclamation,msgTitle
61  End If
62  
63  oVirtDir.SetInfo
64  If Err <> 0 Then
65   MsgBox "更新虚拟目录信息失败!" & vbCrLf & Err.Description,vbOKOnly&vbExclamation,msgTitle
66  End If
67 End Sub
68 
69 Call AppMap

上面代码的缺点是只能面对一个站点的时候,如果安装的时候服务器有多个站点,代码还需要进行改进。

 

posted @ 2012-03-03 17:10  Popcorn  阅读(1648)  评论(2编辑  收藏  举报