This project is read-only.

Default Routes and Errors

Topics: Web Api
May 12, 2011 at 10:55 PM

First let me say that I am very impressed with the Web Api and it's extensibility. I've managed to create the RESTful API prototype we've been itching to develop in a rather short amount of time.
Everything has been pretty straight forward as far as MediaTypeFormatters, DelegatingChannels, etc...

Now I'm trying to clean some things up, as far as 404s and 500s are concerned.

Is there a way to create some kind of default route or something so if a valid endpoint is "http://domain/Invoice" and they contact "http://domain/Invoices" I can return some custom page. 

<customErrors mode="On">
      <error statusCode="404" redirect="/notfound.htm"/>

Or even better, return some custom object or message that run through the Formatters to return some JSON or XML?

May 12, 2011 at 11:09 PM

Yes. You can use wildcards in the programming model to handle those scenarios.

For example you can write a service like this

public class CatchAllService() [
  [WebInvoke(Method="*", UriTemplate="*")]
  public HttpResponseMessage Handle(HttpRequestMessage request) {
    //logic goes here

You would have to register this as the laster service in your routes.

We also do have a custom error handler but it is currently not working right and only allows setting status codes.

May 12, 2011 at 11:10 PM

INeedADip thanks for the feedback btw!