安装MVC项目时自动给IIS添加通配符映射
在IIS6中安装ASP.NET MVC项目时,需要将aspnet_isapi.dll添加到网站虚拟目录的通配符映射当中,很多时候我们需要手动完成。这几天弄了个ASP.NET MVC3的项目,写完后做了一个安装部署程序,想安装的时候能自动将aspnet_isapi.dll添加到虚拟目录的通配符映射当中,于是写了下面的VBS脚本,脚本支持将虚拟目录当作参数传递进来,可以自动给虚拟目录添加通配符映射。可以把这个脚本另存为.vbs文件,然后放到安装类当中使用 System.Diagnostics.Process.Start(vbsFile,virtualPath) 方法进行调用。安装目录可以使用/targetDir=取得,然后处理后获得虚拟目录名。
如何制作安装项目及如何传递参数可以参考:http://kb.iyunv.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()
12Dim oVirtDir
13set oVirtDir = Nothing
14On Error Resume Next
16Set oVirtDir = GetObject("IIS://localhost/W3Svc/1/Root/" & virtualPath)
17If Err <> 0 Then
18 MsgBox "未能创建 IIS 管理对象!" & vbCrLf & Err.Description,vbOKOnly&vbExclamation,msgTitle
19End If
20If Not oVirtDir Is Nothing Then
21 MapHandlers oVirtDir
22Else
23 MsgBox "添加映射失败!",vbOKOnly&vbExclamation,msgTitle
24End If
25On Error GoTo 0
26 End Function
27
28 Sub MapHandlers(oVirtDir)
29Err.Number = 0
30Dim scriptMaps
31scriptMaps = oVirtDir.GetEx("ScriptMaps")
32If Err <> 0 Then
33 MsgBox "未能获取当前脚本映射属性。"& vbCrLf & Err.Description,vbOKOnly&vbExclamation,msgTitle
34 Exit Sub
35End If
36Dim iMap
37Dim sourceMap
38Dim newMap
39newMap = ""
40For 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
49Next
50If sourceMap = "" Then
51 MsgBox "未能找到aspx脚本映射",vbOKOnly&vbExclamation,msgTitle
52 exit sub
53End If
54Redim Preserve scriptMaps(UBound(scriptMaps) + 1)
55newMap = Replace(sourceMap, ".aspx,", "*,")
56scriptMaps(UBound(scriptMaps)) = newMap
57'MsgBox scriptMaps(UBound(scriptMaps))
58oVirtDir.PutEx 2, "scriptMaps", scriptMaps
59If Err <> 0 Then
60 MsgBox "保存脚本映射失败!" & vbCrLf & Err.Description,vbOKOnly&vbExclamation,msgTitle
61End If
62
63oVirtDir.SetInfo
64If Err <> 0 Then
65 MsgBox "更新虚拟目录信息失败!" & vbCrLf & Err.Description,vbOKOnly&vbExclamation,msgTitle
66End If
67 End Sub
68
69 Call AppMap 上面代码的缺点是只能面对一个站点的时候,如果安装的时候服务器有多个站点,代码还需要进行改进。
页:
[1]