返回JSON或部分html的ASP.NETMVC控制器操作我正在嘗試創建控制器操作,它將根據參數返回JSON或部分html。將結果異步返回到MVC頁面的最佳方法是什么?
3 回答

至尊寶的傳說
TA貢獻1789條經驗 獲得超10個贊
if (Request.AcceptTypes.Contains("text/html")) { return View();}else if (Request.AcceptTypes.Contains("application/json")){ return Json( new { id=1, value="new" } );}else if (Request.AcceptTypes.Contains("application/xml") || Request.AcceptTypes.Contains("text/xml")){ //}
$.get(url, null, function(data, textStatus) { console.log('got %o with status %s', data, textStatus); }, "json"); // or xml, html, script, json, jsonp or text
添加回答
舉報
0/150
提交
取消