This project is read-only.

"Implementing a custom ICorsPolicyProviderFactory" always show error in VB project

Topics: ASP.NET Web API
Apr 19, 2013 at 2:13 PM
Edited Apr 19, 2013 at 2:16 PM
hi, I try CORS in Web API for VB and C# project, In "Implementing a custom ICorsPolicyProviderFactory" (from will show error in VB Project.

This's my code:
Imports System.Web.Http.Cors

Public Class CorsConfiguration
Private _settings As New Dictionary(Of String, EnableCorsAttribute)()

Public Sub AddSetting(controller As String, policyProvider As EnableCorsAttribute)
_settings.Add(controller, policyProvider)
End Sub

Public Overridable Function GetPolicyForRequest(controller As String) As EnableCorsAttribute
Dim policyProvider As EnableCorsAttribute = Nothing
_settings.TryGetValue(controller, policyProvider)
Return policyProvider
End Function
End Class
Imports System.Net.Http
Imports System.Web.Http.Cors

Public Class ConfigBasedPolicyProviderFactory
    Implements ICorsPolicyProviderFactory

    Private _configuration As CorsConfiguration

    Public Sub New(configuration As CorsConfiguration)
        _configuration = configuration
    End Sub

    Public Function GetCorsPolicyProvider(request As Net.Http.HttpRequestMessage) As ICorsPolicyProvider Implements ICorsPolicyProviderFactory.GetCorsPolicyProvider
        Dim routeData As Object = request.GetRouteData()
        If routeData = Nothing OrElse Not routeData.Values.Keys.Contains("controller") Then
            Return Nothing
        End If

        Dim controller = TryCast(routeData.Values("controller"), String)
        Return _configuration.GetPolicyForRequest(controller)
    End Function
End Class
Dim corsConfig As New CorsConfiguration()
corsConfig.AddSetting("Values", New EnableCorsAttribute())
corsConfig.AddSetting("Users", New EnableCorsAttribute With {.Origins = New String() {"http://localhost:13466"}})
config.SetCorsPolicyProviderFactory(New ConfigBasedPolicyProviderFactory(corsConfig))
Is my code problem, or Cors.dll?

Apr 19, 2013 at 2:21 PM
I find problem is "Dim routeData As Object", change to "Dim routeData = ..." will fine.