微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

创建 PowerShell 脚本以启用 HTTPCompression 并在网站的配置文件中使用命令行 (appcmd) 添加 MimeType

如何解决创建 PowerShell 脚本以启用 HTTPCompression 并在网站的配置文件中使用命令行 (appcmd) 添加 MimeType

我想启用 httpcompression,然后使用 appcmd 将 mimetypes 添加到 web.config 文件。 我知道我们可以从

applicationHost.config

文件。 IIS7.5以上认开启

我们可以从

%windir%\System32\inetsrv\config\applicationHost.config

但我的要求是使用 appcmd 直接启用和添加 mime 类型到 web.config 文件(基本上覆盖 applicationHost.config 中现有的设置)

解决方法

您可以使用以下命令启用和禁用站点压缩:

appcmd set config "site1" /section:urlCompression /doDynamicCompression:True

appcmd set config "urlsample" /section:urlCompression /doStaticCompression:True

要添加 MIME 类型,请使用以下语法:

appcmd set config /section:staticContent /+"[fileExtension='string',mimeType='string']"

变量 fileExtension 字符串是文件扩展名。变量 mimeType 字符串是 MIME 类型。例如,要创建 MIME 类型,请在命令提示符下键入以下内容,然后按 ENTER:

appcmd set config /section:staticContent /+"[fileExtension='.xyz',mimeType='application/octet-stream']"

有关添加 MIME 类型的更多信息,您可以参考此链接:To add a MIME type

,
$appcmdpath="$env:windir\system32\inetsrv\appcmd.exe"
$path="{PathofHostedAppFromIIS}/"

ECHO 'Remove Existing sections if any this is important if we face issues any issue while it for the first time or running the script mutiple times incase of any error'
& $appcmdpath clear config $path -section:system.webServer/httpCompression /delete:true /commit:app
& $appcmdpath clear config $path -section:system.webServer/urlCompression /delete:true /commit:app

ECHO 'Enable Http Compression'
& $appcmdpath set config $path -section:system.webServer/httpCompression /directory:'%SystemDrive%\inetpub\temp\IIS Temporary Compressed Files' /commit:app

ECHO 'Clear apphost default compression'
& $appcmdpath set config $path -section:system.webServer/httpCompression /~"staticTypes" /commit:app

ECHO 'Add default stypes available in Apphost'
& $appcmdpath set config $path -section:system.webServer/httpCompression /+"staticTypes.[mimeType='text/*',enabled='True']" /commit:app
& $appcmdpath set config $path -section:system.webServer/httpCompression /+"staticTypes.[mimeType='message/*',enabled='True']" /commit:app
& $appcmdpath set config $path -section:system.webServer/httpCompression /+"staticTypes.[mimeType='application/javascript',enabled='True']" /commit:app
& $appcmdpath set config $path -section:system.webServer/httpCompression /+"staticTypes.[mimeType='application/atom+xml',enabled='True']" /commit:app
& $appcmdpath set config $path -section:system.webServer/httpCompression /+"staticTypes.[mimeType='application/xaml+xml',enabled='True']" /commit:app
& $appcmdpath set config $path -section:system.webServer/httpCompression /+"staticTypes.[mimeType='image/svg+xml',enabled='True']" /commit:app

ECHO 'Below script can be added to exclude mime types '
& $appcmdpath set config $path -section:system.webServer/httpCompression /+"staticTypes.[mimeType='image/jpeg',enabled='False']" /commit:app

& $appcmdpath set config $path -section:system.webServer/httpCompression /+"staticTypes.[mimeType='*/*',enabled='False']" /commit:app

ECHO 'Enable URL Compression'
& $appcmdpath set config $path -section:system.webServer/urlCompression /doDynamicCompression:"true"  /doStaticCompression:"true" /commit:app

我们使用 commit:app 在应用程序的 web.config 中添加配置条目 您可以使用 /commit:apphost 在位于

的全局 apphost 文件中添加相同的内容
%windir%\System32\inetsrv\config\applicationHost.config

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。