0

I'm trying to implement a Web API project that exposes some models. However, the way the model is retrieved isn't very RESTful.

1. Instead of having just one ID, it has a combination of 4 different ones to populate the model data by running a stored proc on the server.

Dim balance as New BalanceSheet(uid, mid, eid, fid)

The snippet above will use uid, mid, eid, fid to retrieve the correct record from the database.

How would I implement this in the controller so that /api/Balance?uid=1&mid=2&eid=3&fid=4 maps to GetBalance(ByVal uid As Integer, ByVal mid As Integer, ByVal eid As Integer, ByVal fid As Integer)

2. Additionallly, I want to be able to filter specific members of BalanceSheet model or get some preprocessed object return like so: /api/Balance/FundCredit?uid=1&mid=2&eid=3&fid=4 maps to GetFundcredit(ByVal uid As Integer, ByVal mid As Integer, ByVal eid As Integer, ByVal fid As Integer)

1 Answer 1

2

I solved it following this article: REST vs. RPC in ASP.NET Web API.

I was actually trying to combine two different paradigms it seems - RPC and REST. I initially thought (wrongly) that the api/{controller}/{action}/{id} could only map to functions that return an ActionResult type.

Here's my routing rules:

        routes.MapHttpRoute( _
            name:="RPCApi", _
            routeTemplate:="api/{controller}/{action}"
        )

        routes.MapHttpRoute( _
            name:="RESTApi", _
            routeTemplate:="api/{controller}/{id}", _
            defaults:=New With {.id = RouteParameter.Optional} _
        )

Which map api/balance/?uid=1&mid=2&eid=3&fid=4 and api/balance/fundcredit?uid=1&mid=2&eid=3&fid=4 to these two functions respectively:

      Public Function GetBalance(ByVal uid As Integer, ByVal mid As Integer, ByVal eid As Integer, ByVal fid As Integer)
        Try
            Dim balance As New BalanceSheet(uid, mid, eid, fid)

            Return balance
        Catch ex As Exception
            Return Nothing
        End Try
    End Function

    Public Function FundCredit(ByVal uid As Integer, ByVal mid As Integer, ByVal eid As Integer, ByVal fid As Integer)
        Dim balance As New BalanceSheet(uid, mid, eid, fid)

        Return balance.FundCredit
    End Function
Sign up to request clarification or add additional context in comments.

1 Comment

Found an arguably "safer" technique that explicitly maps actions and methods together, here: haacked.com/archive/2008/08/29/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.