locked
Web API 2 UriPathExtensionMapping with attribute routing RRS feed

  • Question

  • User-296704440 posted

    I want to write a service call such as http://localhost:nnnnn/api/attendance.xml/1 or http://localhost:nnnnn/api/attendance.json/1

    ... so that correct formatter is automatically applied.

    Public Module WebApiConfig
        Public Sub Register(ByVal config As HttpConfiguration)
            ' Web API configuration and services
    
            ' Web API routes - this turns on attribute routing
            config.MapHttpAttributeRoutes()
    
            ' No longer needed as we are specifying Attribute Routing in the Controller (and above)
            'config.Routes.MapHttpRoute(
            '    name:="APIWithID",
            '    routeTemplate:="api/{controller}.{ext}/{id}",
            '    defaults:=New With {.id = RouteParameter.Optional, .ext = RouteParameter.Optional})
    
            config.Formatters.JsonFormatter.AddUriPathExtensionMapping("json", "application/json")
            config.Formatters.XmlFormatter.AddUriPathExtensionMapping("xml", "application/xml")
    
        End Sub
    End Module

    Here is my controller.

    Imports System.Web.Http
    Public Class AttendanceController
        Inherits ApiController
    
        ' Proves useful information
        ' https://www.asp.net/web-api/overview/web-api-routing-and-actions/attribute-routing-in-web-api-2
    
        <Route("api/attendance.{ext}/")>
        Public Function GetThemAll(ext As String) As IHttpActionResult
            Dim Dude As New Person("Jones")
            Return Ok(Dude)
        End Function
        <Route("api/attendance.{ext}/{id:int?}")>
        Public Function GetJustOne(ext As String, Optional id As Integer = 0) As IHttpActionResult
            Dim Dude As New Person("Lewis")
            Return Ok(Dude)
        End Function
    
    End Class

    It is my understanding that the 'ext' parameter is somehow mapped to the uri path extension mapping?

    Perhaps I should confirm the boiler plate to call the Register routine is still in place.

    Imports System.Web.Http
    
    Public Class WebApiApplication
        Inherits System.Web.HttpApplication
    
        Protected Sub Application_Start()
            GlobalConfiguration.Configure(AddressOf WebApiConfig.Register)
        End Sub
    End Class

    The behaviour of this code is as follows (with either Chrome or IE11).

    http://localhost:nnnnn/api/attendance.xml - fails 404

    http://localhost:nnnnn/api/attendance.xml/ - provides json

    {"Name":"Jones"}

    http://localhost:nnnnn/api/attendance.xml/1 - provides json

    {"Name":"Lewis"}

    http://localhost:nnnnn/api/attendance.json/1 - provides json (expected)

    {"Name":"Lewis"}

    I am not expecting this to work - all it does is serves to illustrate that the ext is completely ignored http://localhost:nnnnn/api/attendance.csv/1

    {"Name":"Lewis"}

    The ext parameter comes through to the controller routine so I could just code the return value based on the parameter - but I thought that was what 

    AddUriPathExtensionMapping

    was supposed to do for me?

    Wednesday, November 16, 2016 11:48 AM

All replies