Corbo152
Corbo152

Reputation: 121

Change key in web.config to hold multiple values

I wish to change the web.config so that the key will hold multiple values:

i have now amended the code as suggested,

what should happen is that if the product SKU starts with either O-GREET or O-PEGC then a punchout module will launch, if not the product is added to basket as normal,

in the web.config file i have:

<add key="PunchOutOnSKUPrefix" value="O-GREET,O-PEGC"/>

and in the relevant controller (ShoppingCartCOntroller)

Extensions.PunchOut punchOut = new Extensions.PunchOut();

            Boolean isPunchOut;
            String id = productVariant.Sku;
            String ticketId = null;

            // Check that the product supports Punch out integration by looking at the first 3 letters of its SKU

            if (String.IsNullOrWhiteSpace(id))
            {
                isPunchOut = false;
            }
            else
            {
                option = id.Substring(0, 7);
                isPunchOut = ConfigurationManager.AppSettings["PunchOutOnSKUPrefix"].Split(',').DefaultOrNull(s => s.Equals(option));
        }

Upvotes: 0

Views: 755

Answers (1)

Konrad Kokosa
Konrad Kokosa

Reputation: 16898

Split returns an array of options, if you want to search for specific value, use:

var option = id.Substring(0, 7);
var isPunchOut = ConfigurationManager.AppSettings["PunchOutOnSKUPrefix"].Split(',').DefaultOrNull(s => s.Equals(option));

If you want to check if id starts with anyone of the values, use:

var isPunchOut = ConfigurationManager.AppSettings["PunchOutOnSKUPrefix"].Split(',').Any(s => id.StartsWith(s));

Upvotes: 1

Related Questions