我有一個現有的ASP.NET MVC的網站,像這樣的動作:大廈的API在ASP.NET MVC網站
public ActionResult Show(int id)
{
var customer = GetCustomer(id);
return View(new ShowCustomerModel(customer));
}
我現在需要執行這些操作的API的一部分的能力,這將是從第三方應用程序調用。理想情況下,動作看起來像:
public ActionResult Get(int id)
{
var customer = GetCustomer(id);
return Json(new CustomerResource(customer));
}
的問題是,什麼ASP.NET MVC的工具或模式存在,讓我將這些結合在一起 - 比如,Rails允許我指定多個返回格式:
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
這是一個很好的模式嗎?我應該也許只是有:
public Customer Get(int id)
{
return GetCustomer(id);
}
並使用操作篩選器選擇性呈現爲JSON或視圖?
這是一個非常酷的方法! –