问题
I have the following transform written in the live config of my web.config.
<system.webServer>
<rewrite xdt:Transform="Insert">
<rules>
<rule name="httpsrewrite">
<match url=".*" />
<serverVariables>
<set name="SERVER_PORT" value="443" />
<set name="HTTPS" value="on" />
</serverVariables>
<action type="None" />
</rule>
</rules>
</rewrite>
</system.webServer>
There is no element in my web.config.
The transform just does not work. All my other transform (replace for elmah and connection string) work fine.
回答1:
Without seeing your source files it's hard to give a definitive answer.
In your web.config file you must have a /configuration/system.webServer element present for the transform to work. If you do not have it then there is no element for which to insert the /configuration/system.webServer/rewrite element.
If this doesn't help, please post at least the structure of both your web.config and the transform file. Also I suggest you install the SlowCheetah VS extension which is great for troubleshooting / previewing transforms.
回答2:
I found that the xdt:Locator and xdt:Transform still works on the <rewrite> elements even though Visual Studio still generates warning messages (The 'http://schemas.microsoft.com/XML-Document-Transform:Locator' attribute is not declared).
<system.webServer>
<rewrite>
<rules>
<clear />
<rule name="Service Only Request Blocking Rule 1" stopProcessing="true" xdt:Locator="Match(name)" xdt:Transform="Replace">
<match url=".*" />
<conditions>
<add input="{URL}" pattern="\/address\/search\/.*$" />
</conditions>
<action type="CustomResponse" statusCode="403" statusReason="Forbidden: Access is denied." statusDescription="You do not have permission to complete this operation." />
</rule>
</rules>
</rewrite>
</system.webServer>
来源:https://stackoverflow.com/questions/24182473/xdttransform-insert-not-working-for-rewrite-in-system-webserver