Setting up RPC Routing for WebAPI

The MVC4 webAPI has a default routing for CRUD operations where it is setup well to work with each controller having limited functions, so Actions are not passed to the controller. I needed to setup RPC style webAPI project that could have many calls to a single controller and then differentiate which action to take based on the specified action specified in the url. After a lot of headaches, I found that you can edit the App_Start/webAPIConfig.cs file from:

 

public static void Register(HttpConfiguration config)
{

      config.Routes.MapHttpRoute(
       name: “DefaultApi”,
       routeTemplate: “api/{controller}/{id}”,
        defaults: new { id = RouteParameter.Optional }
        );
// Uncomment the following line of code to enable query support for actions with an IQueryable or IQueryable<T> return type.

// To avoid processing unexpected or malicious queries, use the validation settings on QueryableAttribute to validate incoming queries.
// For more information, visit http://go.microsoft.com/fwlink/?LinkId=279712.
//config.EnableQuerySupport();

// To disable tracing in your application, please comment out or remove the following line of code
// For more information, refer to: http://www.asp.net/web-api
config.EnableSystemDiagnosticsTracing();
}

 

to:

 

public static void Register(HttpConfiguration config)
{

           config.Routes.MapHttpRoute(
           name: “RestPCApi”,
           routeTemplate: “{controller}/{action}”,
           defaults: new { action = RouteParameter.Optional }
            );

           config.Routes.MapHttpRoute(
           name: “DefaultApi”,
           routeTemplate: “{controller}/{action}/{id}”, // URL with parameters
           defaults: new { controller = “api”, RouteParameter.Optional } // Parameter defaults
           );

// Uncomment the following line of code to enable query support for actions with an IQueryable or IQueryable<T> return type.
// To avoid processing unexpected or malicious queries, use the validation settings on QueryableAttribute to validate incoming queries.
// For more information, visit http://go.microsoft.com/fwlink/?LinkId=279712.
//config.EnableQuerySupport();

// To disable tracing in your application, please comment out or remove the following line of code
// For more information, refer to: http://www.asp.net/web-api
config.EnableSystemDiagnosticsTracing();
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s