[转载]安装MVC项目时自动给IIS添加通配符映射 – Popcorn – 博客园.
在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脚本程序:
Option Explicit
Dim virtualPath
Dim msgTitle
msgTitle = "添加脚本映射"
If WScript.Arguments.Length > 0 Then
virtualPath = WScript.Arguments(0)
Else
virtualPath = "Web"
End If
Public Function AppMap()
Dim oVirtDir
set oVirtDir = Nothing
On Error Resume Next
Set oVirtDir = GetObject("IIS://localhost/W3Svc/1/Root/" & virtualPath)
If Err <> 0 Then
MsgBox "未能创建 IIS 管理对象!" & vbCrLf & Err.Description,vbOKOnly&vbExclamation,msgTitle
End If
If Not oVirtDir Is Nothing Then
MapHandlers oVirtDir
Else
MsgBox "添加映射失败!",vbOKOnly&vbExclamation,msgTitle
End If
On Error GoTo 0
End Function
Sub MapHandlers(oVirtDir)
Err.Number = 0
Dim scriptMaps
scriptMaps = oVirtDir.GetEx("ScriptMaps")
If Err <> 0 Then
MsgBox "未能获取当前脚本映射属性。"& vbCrLf & Err.Description,vbOKOnly&vbExclamation,msgTitle
Exit Sub
End If
Dim iMap
Dim sourceMap
Dim newMap
newMap = ""
For iMap = LBound(scriptMaps) To UBound(scriptMaps)
If Left(scriptMaps(iMap), 6) = ".aspx," Then
sourceMap = scriptMaps(iMap)
'MsgBox "Found aspx: " & newMap
End If
If Left(scriptMaps(iMap), 3) = ".*," Then
'MsgBox "已经添加了映射"
Exit Sub
End If
Next
If sourceMap = "" Then
MsgBox "未能找到aspx脚本映射",vbOKOnly&vbExclamation,msgTitle
exit sub
End If
Redim Preserve scriptMaps(UBound(scriptMaps) + 1)
newMap = Replace(sourceMap, ".aspx,", "*,")
scriptMaps(UBound(scriptMaps)) = newMap
'MsgBox scriptMaps(UBound(scriptMaps))
oVirtDir.PutEx 2, "scriptMaps", scriptMaps
If Err <> 0 Then
MsgBox "保存脚本映射失败!" & vbCrLf & Err.Description,vbOKOnly&vbExclamation,msgTitle
End If
oVirtDir.SetInfo
If Err <> 0 Then
MsgBox "更新虚拟目录信息失败!" & vbCrLf & Err.Description,vbOKOnly&vbExclamation,msgTitle
End If
End Sub
Call AppMap
上面代码的缺点是只能面对一个站点的时候,如果安装的时候服务器有多个站点,代码还需要进行改进。
Mikel