I have an existing ASP.NET MVC website with actions like this:
public ActionResult Show(int id)
{
var customer = GetCustomer(id);
return View(new ShowCustomerModel(customer));
}
I now need the ability to perform these actions as part of an API, which will be called from third party applications. Ideally the action would look like:
public ActionResult Get(int id)
{
var customer = GetCustomer(id);
return Json(new CustomerResource(customer));
}
The question is, what ASP.NET MVC tools or patterns exist that allow me to combine these together - for example, Rails allows me to specify multiple return formats:
def index
@customer = get_customer(...)
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @customer}
format.json { render :json => @customer}
end
end
Is that even a nice pattern? Should I perhaps just have:
public Customer Get(int id)
{
return GetCustomer(id);
}
And use action filters to selectively render as JSON or a view?