如何:設定 IIS 5.0 和 IIS 6.0 以部署 WPF 應用程式

只要有適當的 Multipurpose Internet Mail Extensions (MIME) 類型設定,您就可以從大部分網路伺服器部署 Windows Presentation Foundation (WPF) 應用程式。 根據預設,Microsoft Internet Information Services (IIS) 7.0 是以這些 MIME 類型來設定,但 Microsoft Internet Information Services (IIS) 5.0 和 Microsoft Internet Information Services (IIS) 6.0 則不是。

本主題說明如何設定 Microsoft Internet Information Services (IIS) 5.0 和 Microsoft Internet Information Services (IIS) 6.0 來部署 WPF 應用程式。

注意

您可以檢查登錄中的 UserAgent 字串,以判斷系統是否已安裝 .NET Framework。 如需詳細資訊,以及可檢查 UserAgent 字串以判斷系統上是否已安裝 .NET Framework 的指令碼,請參閱 偵測有無安裝 .NET Framework 3.0

調整內容到期設定

您應該將內容到期設定調整為 1 分鐘。 下列程序概述如何使用 IIS 執行此作業。

  1. 按一下 [開始] 功能表,指向 [系統管理工具],然後按一下 [Internet Information Services (IIS) 管理員]。 您也可以從命令列使用 "%SystemRoot%\system32\inetsrv\iis.msc" 來啟動此應用程式。

  2. 展開 IIS 樹狀目錄,直到找到 預設的網站 節點。

  3. 以滑鼠右鍵按一下 [預設的網站],然後從操作功能表選取 [屬性]

  4. 選取 [HTTP 標頭] 索引標籤,然後按一下 [啟用內容到期日]。

  5. 設定內容於 1 分鐘後過期。

註冊 MIME 類型和副檔名

您必須註冊數種 MIME 類型和副檔名,用戶端系統上的瀏覽器才能載入正確的處理常式。 您需要新增下列類型:

副檔名 MIME 類型
.manifest application/manifest
.xaml application/xaml+xml
應用程式。 application/x-ms-application
.xbap application/x-ms-xbap
.deploy application/octet-stream
.xps application/vnd.ms-xpsdocument

注意

您不需要在用戶端系統上註冊 MIME 類型或副檔名。 當您安裝 Microsoft .NET Framework 時,會自動註冊這些項目。

下列 Microsoft Visual Basic Scripting Edition (VBScript) 範例會自動將必要的 MIME 類型新增至 IIS。 若要使用指令碼,請將程式碼複製到伺服器上的 .vbs 檔案。 然後,從命令列執行該檔案,或在 Microsoft Windows Explorer 中按兩下該檔案,以執行指令碼。

' 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

注意

多次執行此指令碼,會在 Microsoft Internet Information Services (IIS) 5.0 或 Microsoft Internet Information Services (IIS) 6.0 metabase 中建立多個 MIME 對應專案。

執行此指令碼後,您可能看不到來自 Microsoft Internet Information Services (IIS) 5.0 或 Microsoft Internet Information Services (IIS) 6.0 Microsoft Management Console (MMC) 的其他 MIME 類型。 不過,這些 MIME 類型已新增至 Microsoft Internet Information Services (IIS) 5.0 或 Microsoft Internet Information Services (IIS) 6.0 metabase。 下列指令碼會顯示 Microsoft Internet Information Services (IIS) 5.0 或 Microsoft Internet Information Services (IIS) 6.0 metabase 中所有 MIME 類型。

' This script lists the MIME types for an IIS Server.
' To use this script, just double-click or execute it from a command line
' by calling cscript.exe

dim mimeMapEntry, allMimeMaps

' Get the MimeMap object.
Set mimeMapEntry = GetObject("IIS://localhost/MimeMap")
allMimeMaps = mimeMapEntry.GetEx("MimeMap")

' Display the mappings in the table.
For Each mimeMap In allMimeMaps
    WScript.Echo(mimeMap.MimeType & " (" & mimeMap.Extension + ")")
Next

請將指令碼儲存為 .vbs 檔案 (例如 DiscoverIISMimeTypes.vbs),然後從命令提示字元使用下列命令來執行:

cscript DiscoverIISMimeTypes.vbs