you’ll need to add newtonsoft json to your project.
Application start, add the initialisation code
1 2 3 |
Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs) '''for the API GlobalConfiguration.Configure(AddressOf WebApiConfig.Register) |
Now the code itself.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
Imports System.Net.Http.Headers Imports System.Web.Http Imports System.Net.Http Public Module WebApiConfig Sub Register(config As System.Web.Http.HttpConfiguration) System.Web.Http.HttpConfigurationExtensions.MapHttpAttributeRoutes(GlobalConfiguration.Configuration) config.Formatters.Insert(0, New JsonFormatter) config.Formatters.JsonFormatter.SupportedMediaTypes.Add(New MediaTypeHeaderValue("text/html")) End Sub ''' <summary> ''' This class ensures that default api requestes come down as JSON ''' </summary> Public Class JsonFormatter Inherits System.Net.Http.Formatting.JsonMediaTypeFormatter Public Sub BrowserJsonFormaater() Me.SupportedMediaTypes.Add(New MediaTypeHeaderValue("text/html")) Me.SerializerSettings.Formatting = Newtonsoft.Json.Formatting.Indented End Sub Public Overrides Sub SetDefaultContentHeaders(type As Type, headers As HttpContentHeaders, mediaType As MediaTypeHeaderValue) MyBase.SetDefaultContentHeaders(type, headers, mediaType) headers.ContentType = New MediaTypeHeaderValue("application/json") End Sub End Class End Module |