1

你能告诉我下面的代码有什么问题吗,我得到了Value of type '2-dimensional array of String' cannot be converted to 'System.Collections.Generic.Dictionary(Of String, String)'

VB.net 代码:

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"}
              }
            })

这是我用作起点的 C# 代码:

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

非常感谢任何建议


以下工作正常:

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
            })
4

2 回答 2

1

您需要使用 From 关键字调用 Dictionary 构造函数:

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"}
   }
})

请参阅集合初始值设定项 (Visual Basic)

于 2013-07-29T17:54:51.110 回答
1

您需要创建一个字典,如:

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

请参阅:如何:使用集合初始化程序初始化字典(C# 编程指南)

于 2013-07-24T14:36:54.980 回答