4

I have done quite a lot of research into ASP.NET Web API - having a RESTful, verb-based approach as well as having a RPC, traditional approach like on ASP.NET MVC together. I am wondering what the neatest approach would be. So far I have found two that work but I am unsure what the best course of action would be.

Rationale

There is a main resource, Patient, which fit perfectly with GET, POST and PUT. However I also have some scenarios where I have a controller with multiple actions, none that really fit in with the verb-based actions, and often multiple actions that don't have any arguments that I could implement route-based constraints (e.g. https://stackoverflow.com/a/14350711/1061602) for.

Approach 1

Multiple routes, different route name, same route template

WebApiConfig routes:

        config.Routes.MapHttpRoute(
            name: "RestApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );


        config.Routes.MapHttpRoute(
            name: "RpcApi",
            routeTemplate: "api/{controller}/{action}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );

REF: https://stackoverflow.com/a/12367487/1061602

Downside, have to add ActionName attribute to each action, otherwise it doesn't work (??):

    [ActionName("RestApi")]
    public object Get(string id)
    {
        ...
    }

    [ActionName("RpcApi")]
    public void Foo()
    {
        ...
    }

Approach 2

Multiple routes, same route name, different route template

WebApiConfig routes:

        config.Routes.MapHttpRoute(
            name: "RestApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );

        config.Routes.MapHttpRoute(
            name: "RpcApi",
            routeTemplate: "rpc/{controller}/{action}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );

REF: http://encosia.com/rest-vs-rpc-in-asp-net-web-api-who-cares-it-does-both/

Downside being that you then have effectively 2 URLs used on the client, one for RESTful actions, one for RPC actions.

Other approaches??

??

Are there any more elegant ways of achieving this?

Community
  • 1
  • 1
Adam Marshall
  • 3,010
  • 9
  • 42
  • 80

1 Answers1

2

Have you looked into Web API 2 attribute routing? you could keep using the RestApi conventional route for most of your application and whereever you have special controllers which do not fit into this model, you can decorate those controllers with attribute routes.

You can take a look at the following WebAPI 2 Attribute Routing sample which tries different scenarios:

http://aspnet.codeplex.com/SourceControl/latest#Samples/WebApi/WebApiAttributeRoutingSample/Readme.txt

Kiran
  • 56,921
  • 15
  • 176
  • 161
  • This looks like a really useful addition, thanks I will take a look! – Adam Marshall Oct 25 '13 at 12:39
  • 1
    As this question has had quite a few views, AttributeRouting really is the way to go. Set up your main route with your main RESTful architecture then if you have any others just decorate them with [Route("api/whatever")] – Adam Marshall Feb 10 '15 at 09:46