MVC naming conventions for JSON actions MVC naming conventions for JSON actions json json

MVC naming conventions for JSON actions


Arguably the path names could all be the same. You can examine the Accept header for the mime-type of your client's desired response, and then return an appropriate view based on what you find there:

  • application/json: JSON View
  • text/xml: XML View
  • text/plain, text/html: JSPView

Browsers set this field to HTML; your JSON clients would simply set this field as appropriate.


It's highly unlikely that anyone would be bookmarking a URL that requests JSON so I think that it's not that important to keep the URL as clean. It's also likely to be programmatically generated, not hand entered. Given these, I'd consider adding it as a query parameter.

 /things/list  -- HTML /things/list?format=json  -- JSON 

This won't break your URLs if you do have ID parameters or need other parameters as well. It could also work with POSTs as well as GETs.

/things/1  -- HTML for "thing 1"/things/1?format=json -- JSON for "thing 1"


I use the convention of

/things/list -- HTML/things/_listpage -- AJAX

The rule is that all AJAXed actions/views have a leading underscore. This tells me that they are never called top-level, and usually have no master page associated. In this case, I keep the action under the same controller in order to share any associated logic.

Typically in the list view I would have a

<% RenderAction("_listpage", "things", new {page = ViewData["CURRENT_PAGE"]}); %>