This project is read-only.

Webapi RouteData inspection

May 7, 2012 at 5:41 PM

I wrote a simple HttpModule (that I can publish on my blog), to dump the current used route both for mvc pages and webapi requests.
The module just let execute the request from the default handler and than ask the handler the route that was used.

The important thing is that I want to dump the Route on the server side and not on the client side as the convenient extension by Haacked already does.
Server side is vital for WebApi as I want to know if I picked the correct route from an asynchronous request/post/etc. using jquery or httpclient from Metro.

I can obtain the route infos for MvcHandler but HttpControllerHandler is totally closed and I had to use reflection on private fields and properties, solution that I don't like at all even if it's for my debugging purposes only.

My wishes:

  1. Expose public properties in HttpControllerHandler to obtain the default/current Route, Values, etc. that have been selected from the handler.
  2. Possibly refactor HttpControllerHandler so that properties in MvcHandler and HttpControllerHandler properties names/base-types are the same.
  3. Maybe a common interface for HttpControllerHandler and MvcHandler is too much to ask, but it would be far more elegant way to inspect the values and the route.

Current problem:
Even using reflection currently I cannot dump some of the values (they are not inside RouteData.Values).
For example:
Url request: /api/customers?id=ALFKI
Route used: api/{controller}/{id}
RouteData.Values:  [controller, customers]; [id, ]
I don't have access to ALFKI as the value of id as it is in the querystring
If the Url request is /api/cuistomers/alfki, the RouteData.Values contains the id as expected.

For my purpose, these data can be readonly but I did not evaluate other possible scenarios.