0

我正在尝试更改 ASP.NET 临时文件的位置,以便在发布新版本期间清理它们。

因为很难找到特定网站的 ASP.NET 临时文件的位置,所以在这些C:\Windows\Microsoft.NET\Framework C:\Windows\Microsoft.NET\Framework64位置下应用虚拟目录我决定将文件移动到磁盘上的特定位置会更容易,然后可以清洁。

您可以通过修改配置部分中的tempDirectory属性来完成此操作。system.web/compilation

我们的服务器构建和发布过程是自动化的,因此将代码添加到配置和发布脚本中看起来很简单。

然而,在测试过程中,我发现 32 位应用程序的位置并没有改变。

我正在使用的代码是:

Set-WebConfigurationProperty -pspath 'MACHINE/WEBROOT' -location 'MyWebSite' -filter 'system.web/compilation' -name 'tempDirectory' -value 'E:\Temporary ASP.NET Files\MyWebSite' -Clr v2.0

此代码正常工作,并将条目写入根 web.config 文件中:C:\Windows\Microsoft.NET\Framework64\v2.0.50727\CONFIG\web.config

例如

<location path="MyWebSite">
  <system.web>
    <compilation tempDirectory="E:\Temporary ASP.NET Files\MyWebSite" />
  </system.web>
</location>

请注意,如果没有 -Clr v2.0 参数,该值将写入 CLR 4.0 配置文件的C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Config\web.config.

您还可以在 IIS 配置编辑器中看到该条目:IIS 配置编辑器显示指定的临时路径

问题是应用程序池设置为“启用 32 位应用程序”,因此该属性被忽略。

如果我手动将上面显示的位置元素C:\Windows\Microsoft.NET\Framework64\v2.0.50727\CONFIG\web.configC:\Windows\Microsoft.NET\Framework\v2.0.50727\CONFIG\web.config

更改有效,并且确实 ASP.NET 临时文件移动到了网站编译时指定的这个位置。

问题不在于 ASP.NET 临时文件,而是一个更普遍的问题,即您打算如何在 PowerShell 或 IIS 中配置 32 位应用程序?似乎没有办法做到这一点,我觉得这很不可思议。仍有大量 32 位应用程序需要配置。

另请注意,我选择使用根 web.config 来存储此值是有充分理由的:

  1. ApplicationHost.config无法存储system.web配置
  2. 临时文件的位置在设计时是未知的,由托管服务器配置决定。因此,无法在web.config应用程序本身的文件中指定此设置。

感谢您对这个问题的任何启发。

4

2 回答 2

1

有两种解决方法。

  1. 使用 32 位 appcmd.exe。这是示例。

%windir%\syswow64\cmd.exe %windir%\syswow64\inetsrv\appcmd.exe 设置配置-section:appSettings /+"[key='test',value='test']" /commit:webroot /clr: 4.0

  1. 直接在 powershell 上使用 MWA。这是示例。

$iis = 新对象 Microsoft.Web.Administration.ServerManager $wcm = 新对象 -TypeName Microsoft.Web.Administration.WebConfigurationMap -ArgumentList "C:\Windows\Microsoft.NET\Framework\v4.0.30319\CONFIG\machine. config","C:\Windows\Microsoft.NET\Framework\v4.0.30319\CONFIG\web.config" $iis.GetWebConfiguration($wcm, $null).GetSection("appSettings").SetAttributeValue("file", "test3") $iis.CommitChanges()

于 2016-01-05T20:17:49.350 回答
0

经过实验,使用此处提供的答案中的一些信息和离线对话,我可以肯定地说,使用 Microsoft WebAdministration PowerShell cmdlet 编辑 32 位根 web.config 文件是不可能的。

似乎 cmdlet 被硬编码为仅查看 64 位版本的配置文件。IIS 管理器的行为方式相同。为什么会这样,我无法解释。

我还发现使用一些用于编辑 64 位 Clr 2.0 网站和应用程序的 cmdlet 存在很多问题。并非所有 cmdlet 上都存在 Clr 参数,即使在它所在的位置上,它似乎也并不总是有效。

因此,我决定放弃 WebAdministration cmdlet 并直接使用“Microsoft.Web.Administration.dll”程序集和Microsoft.Web.Administration.ServerManager对象。

以下是我编写的一些可能有用的函数:

    function Get-MWAConfigObjects
    {
        <#
                .SYNOPSIS
                Returns object to manage IIS configuration in root web.config
                .DESCRIPTION
                The objects returned allow viewing or editing or configuration. Parameters open the appropriate version, either 32 or 64 bit for the appropriate version of the ManagedRunTime. https://msdn.microsoft.com/en-us/library/microsoft.web.administration.servermanager(v=vs.90).aspx
Ensure that you call CommitChanges to save any changes made.
                .EXAMPLE
                $MWA = Get-MWAConfigObjects -ManagedRunTimeVersion v2.0 -Architecture 32               $MWA.Configuration.GetSection('system.web/compilation','MyWebSite/MyApplication').SetAttributeValue('tempDirectory', 'C:\NewPath')
                $MWA.ServerManager.CommitChanges()             
        #>
        [cmdletbinding(positionalbinding = $false)]
        param(
            [Parameter(Mandatory = $True)][string][ValidateSet('v2.0','v4.0')] $ManagedRunTimeVersion,
            [Parameter(Mandatory = $True)][string][ValidateSet(32,64)] $Architecture
        )

        $assemblyPath = $(Join-Path -Path $([System.Environment]::GetFolderPath('System')) -ChildPath $(Join-Path -Path 'inetsrv' -ChildPath 'Microsoft.Web.Administration.dll'))
        If (Test-Path -Path $assemblyPath -PathType Leaf)
        {
            $null = [System.Reflection.Assembly]::LoadFrom($assemblyPath)
            $iis = New-Object -TypeName Microsoft.Web.Administration.ServerManager
            $wcm = New-Object -TypeName Microsoft.Web.Administration.WebConfigurationMap -ArgumentList $(Get-ConfigFilePath -Type machine -ManagedRunTimeVersion $ManagedRunTimeVersion -Architecture $Architecture), $(Get-ConfigFilePath -Type web -ManagedRunTimeVersion $ManagedRunTimeVersion -Architecture $Architecture)
            $configuration = $iis.GetWebConfiguration($wcm, $null)

            $object = New-Object -TypeName PSObject
            $object | Add-Member -MemberType NoteProperty -Name ServerManager -Value $iis
            $object | Add-Member -MemberType NoteProperty -Name Configuration -Value $configuration
            Write-Output -InputObject  $object
        }
        else
        {
            Throw "Cannot validate existence of required assembly 'Microsoft.Web.Administration.dll' at ""$assemblyPath"""
        }
    }

    function Get-ConfigFilePath
    {
        [CmdletBinding(PositionalBinding = $false)]
        param
        (
            [Parameter(Mandatory = $True)][string][ValidateSet('web','machine')] $Type, 
            [Parameter(Mandatory = $True)][string][ValidateSet('v2.0','v4.0')] $ManagedRunTimeVersion, 
            [Parameter(Mandatory = $True)][string][ValidateSet(32,64)] $Architecture
        )

        $ErrorActionPreference = 'stop'

        switch ($ManagedRunTimeVersion)
        {
            'v2.0'
            {
                switch ($Architecture)
                {
                    32
                    {
                        $path = $(Join-Path -Path $([System.Environment]::GetFolderPath('Windows')) -ChildPath "Microsoft.NET\Framework\v2.0.50727\CONFIG\$Type.config")
                        break
                    }
                    64
                    {
                        $path = $(Join-Path -Path $([System.Environment]::GetFolderPath('Windows')) -ChildPath  "Microsoft.NET\Framework64\v2.0.50727\CONFIG\$Type.config")
                        break
                    }
                }

                break;
            }
            'v4.0'
            {
                switch ($Architecture)
                {
                    32
                    {
                        $path = $(Join-Path -Path $([System.Environment]::GetFolderPath('Windows')) -ChildPath "Microsoft.NET\Framework\v4.0.30319\CONFIG\$Type.config")
                        break
                    }
                    64
                    {
                        $path = $(Join-Path -Path $([System.Environment]::GetFolderPath('Windows')) -ChildPath  "Microsoft.NET\Framework64\v4.0.30319\CONFIG\$Type.config")
                        break
                    }
                }

                break;
            }
        }

        If (Test-Path -Path $path -PathType Leaf)
        {
            Write-Output -InputObject $path
        }
        else
        {
            Throw "Cannot validate configuration file at path ""$path"""
        }
    }

我希望这可以帮助别人。

于 2016-01-11T18:00:57.730 回答