2

On Azure Portal, I am able to setup path-based rules which have some default setting, and a list of sub-rules (UrlPathMap).

Each of those sub-rules has a name, paths, backend pool, and HTTP setting that have to be configured.

enter image description here

As I can see I can update this map easily through Azure Portal. I want to be able to create such sub-rules dynamically from code as part of the application installation. I would prefer to do this directly from .NET (ASP.NET Core 3.1) application, but Azure CLI or Azure Powershell script should be OK for me as well.

At this point, I tried to use Microsoft.Azure.Management.Fluent library, Azure CLI and Azure Powershell but I do not see the direct option to do what is need.

Will be really glad to get some help here.

Yurii Horak
  • 93
  • 1
  • 7

1 Answers1

3

According to my test, we can use the following PowerShell script to create a sub-rule.

Connect-AzAccount

$groupName=""
$gatewayName=""
$poolNmae=""
$httpName=""
$pathRuleName=""


# get original sub-rule in your path rule
$appgateway=Get-AzApplicationGateway -Name $gatewayName -ResourceGroupName $groupName
$pathmap=Get-AzApplicationGatewayUrlPathMapConfig -ApplicationGateway $appgateway -Name $pathRuleName
$t =$pathmap.PathRules.ToArray()

# add a new sub-rule to the path rule
#  1. get the require backendpool or backendhttp settings
$pool=Get-AzApplicationGatewayBackendAddressPool -Name $poolNmae -ApplicationGateway $appgateway
$http=Get-AzApplicationGatewayBackendHttpSetting -Name $httpName -ApplicationGateway $appgateway
#  2. create the sub-rule
$r=New-AzApplicationGatewayPathRuleConfig -Name "rule01" -Paths "/path" -BackendAddressPool  $pool -BackendHttpSettings $http
$t += $r
#  3. update the path rule to add the new sub rule
Set-AzApplicationGatewayUrlPathMapConfig -ApplicationGateway $appgateway -Name $pathmap.Name -PathRules $t -DefaultBackendAddressPool $pool -DefaultBackendHttpSettings $http
#  4. make the update effective
Set-AzApplicationGateway -ApplicationGateway $appgateway

enter image description here

Jim Xu
  • 21,610
  • 2
  • 19
  • 39