原文 自动注册 IIS6 的 MIME 类型
由于IIS5和IIS6有很多的MIME类型没有设置,其中还包括了FLV(video/x-flv),上篇文章描述了制作《IIS6 自动安装》,而它也仅仅实现了IIS的自动安装。像注册 ASP.NET 到 IIS 都没有制作,还有这些MIME的添加,现在就来补充下这些方法。
注册 ASP.NET 到 IIS
一想就很简单了,不就是调用 .net framework 对应的注册程序嘛。你可以写一个批处理程序,在安装 IIS 服务之后执行,代码如下:
if exist %SystemRoot%\Microsoft.NET\Framework\v1.1.4322\aspnet_regiis.exe %SystemRoot%\Microsoft.NET\Framework\v1.1.4322\aspnet_regiis.exe -i -enable
if exist %SystemRoot%\Microsoft.NET\Framework\v2.0.50727\aspnet_regiis.exe %SystemRoot%\Microsoft.NET\Framework\v2.0.50727\aspnet_regiis.exe -iru -enable
自动注册 MIME 类型
回到本文的主题,我们做类似 ClickOnce 部署等需要注册额外的 MIME 才能使 IIS 支持使用(这是IIS的安全策略)。如果是IIS7的话当然就不用设置这些了,他们是 .net framework 3.0 的内容。我从 MSDN 里找到一个 VBS 注册脚本:
' This script adds the necessary Windows Presentation Foundation MIME types
' to an IIS Server.
' To use this script, just double-click or execute it from a command line.
' Running this script multiple times results in multiple entries in the IIS MimeMap.
Dim MimeMapObj, MimeMapArray, MimeTypesToAddArray, WshShell, oExec
Const ADS_PROPERTY_UPDATE = 2
' Set the MIME types to be added
MimeTypesToAddArray = Array(".manifest", "application/manifest", ".xaml", _
"application/xaml+xml", ".application", "application/x-ms-application", _
".deploy", "application/octet-stream", ".xbap", "application/x-ms-xbap", _
".xps", "application/vnd.ms-xpsdocument")
' Get the mimemap object
Set MimeMapObj = GetObject("IIS://LocalHost/MimeMap")
' Call AddMimeType for every pair of extension/MIME type
For counter = 0 to UBound(MimeTypesToAddArray) Step 2
AddMimeType MimeTypesToAddArray(counter), MimeTypesToAddArray(counter+1)
Next
' Create a Shell object
Set WshShell = CreateObject("WScript.Shell")
' Stop and Start the IIS Service
Set oExec = WshShell.Exec("net stop w3svc")
Do While oExec.Status = 0
WScript.Sleep 100
Loop
Set oExec = WshShell.Exec("net start w3svc")
Do While oExec.Status = 0
WScript.Sleep 100
Loop
Set oExec = Nothing
' Report status to user
WScript.Echo "Windows Presentation Foundation MIME types have been registered."
' AddMimeType Sub
Sub AddMimeType (Ext, MType)
' Get the mappings from the MimeMap property.
MimeMapArray = MimeMapObj.GetEx("MimeMap")
' Add a new mapping.
i = UBound(MimeMapArray) + 1
Redim Preserve MimeMapArray(i)
Set MimeMapArray(i) = CreateObject("MimeMap")
MimeMapArray(i).Extension = Ext
MimeMapArray(i).MimeType = MType
MimeMapObj.PutEx ADS_PROPERTY_UPDATE, "MimeMap", MimeMapArray
MimeMapObj.SetInfo
End Sub
第10到16行,是我们要加入的MIME类型,如果你还要加入FLV,就再添加一行,写上:
".flv","video/x-flv", _
保存成 RegisterIISMimeTypes.vbs 后,双击它,或者用命令行调用(可以将此命令行写在批处理中):
cscript.exe RegisterIISMimeTypes.vbs
这样就行了。
如果你想对这方面有所了解,需要掌握以下知识:
· IIS
· IIS MIME 类型
· Windows 脚本宿主(VBS脚本)
· 批处理
· .NET Framework ClickOnce
我将这些有用的文件传到网上,你可以下载研究,文件包中还包括了显示当前IIS MIME类型的VBS,用cscript.exe命令调用。
-------------------------------------------------
进入下载页面
类型: 7Z 压缩文件 大小: 1.8 KB上传时间 2009-4-3 10:34119.112.166.203 参考:http://msdn.microsoft.com/zh-cn/library/ms752346.aspx