views:

70

answers:

4

I'd like to be able to change the extension of a url and recieve the model in a different format.

e.g. if

/products/list

returns a html page containing a list of products, then

/products/list.json

would return them in a json list.

Note: I like the simplicity of the ASP.NET MVC REST SDK, it only requires about 5 lines of code to hook it in, but the format is specified as a query string parameter i.e. /products/list?format=json which is not what I want, I could tweak this code if there are no other options, but I don't want to reinvent the wheel!

A: 

You should be able to just use routes in conjunction with the rest sdk

NickLarsen
Do you know how? I know that it is possible if I tweak the SDK code, but I'd rather not have to do that.
thatismatt
A: 

Instead of "list.json", you could choose "list/json" and use a route like

{controller}/{action}/{id}

Then ProductController.List would be called, with an ID parameter of "json". The .List() action then would decide whether or not to return an HTML view or JSON content.

David Lively
that wouldn't work for an index action e.g. /products and /products/json
thatismatt
+1  A: 

I wrote a blog post that shows one possible example. It's a tiny bit complicated, but might work for your needs.

http://haacked.com/archive/2009/01/06/handling-formats-based-on-url-extension.aspx

Haacked
A: 

If you have the flexibility to drop Apache or something similar in front of your service, you can always use mod_rewrite to rewrite an external http://products/list.json into http://products/list?format=json which your framework can render more easily.

Jon Moore