Question

Can you please tell me what is wrong with the below code, I am getting Value of type '2-dimensional array of String' cannot be converted to 'System.Collections.Generic.Dictionary(Of String, String)'

VB.net code:

MyBase.SetConfig(New EndpointHostConfig() With {
              .GlobalResponseHeaders = {
             {"Access-Control-Allow-Origin", "*"},
             {"Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS"},
             {"Access-Control-Allow-Headers", "Content-Type"}
              }
            })

And here is C# code I used as a starter point:

base.SetConfig(new EndpointHostConfig
    {
        GlobalResponseHeaders = {
            { "Access-Control-Allow-Origin", "*" },
            { "Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS" },
            { "Access-Control-Allow-Headers", "Content-Type" },
        },
    });

Any suggestions much appreciated


The below works OK:

Dim CorsHeaders As New Dictionary(Of String, String)
            CorsHeaders.Add("Access-Control-Allow-Origin", "*")
            CorsHeaders.Add("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS")
            CorsHeaders.Add("Access-Control-Allow-Headers", "Content-Type")

            MyBase.SetConfig(New EndpointHostConfig() With {
               .GlobalResponseHeaders = CorsHeaders
            })
Was it helpful?

Solution

You need to invoke the Dictionary constructor with the From keyword:

MyBase.SetConfig(New EndpointHostConfig() With {
   .GlobalResponseHeaders = New Dictionary(Of String, String) From {
      {"Access-Control-Allow-Origin", "*"},
      {"Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS"},
      {"Access-Control-Allow-Headers", "Content-Type"}
   }
})

See Collection Initializers (Visual Basic).

OTHER TIPS

You need to create a dictionary like:

GlobalResponseHeaders = new Dictionary<string, string>() 
          {
            { "Access-Control-Allow-Origin", "*" },
            { "Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS" },
            { "Access-Control-Allow-Headers", "Content-Type" }
          }

See: How to: Initialize a Dictionary with a Collection Initializer (C# Programming Guide)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top