您可以輕鬆創建一個REST API:
routes.MapRoute(
"Workout", // Route name
"{controller}/{action}", // URL with parameters
new { controller = "Home", action = "Index" } // Parameter defaults
);
,並使用例如你鍛鍊的:
public class WorkoutController : Controller
{
public ActionResult Index()
{
return RedirectToAction("Index", "Help");
}
[HttpPost]
public ActionResult Workout(FormCollection form)
{
// HTTP POST: ADD Workout
// process form and return JSON
return Json(myObject);
}
[HttpDelete]
public ActionResult Workout(string id)
{
// HTTP DELETE: REMOVE Workout
// process form and return JSON
return Json(myObject);
}
[HttpGet]
public ActionResult Workout(string id)
{
// HTTP GET: GET Workout
// process form and return JSON
return Json(myObject);
}
}
但我建議你雖然使用WCF: )
從cl ient方:
$.ajax({
type: "POST",
url: "/Workout/Workout",
data: {
'type': '123456',
'height': '171'
}
success: function(msg){
alert("Data Saved: " + msg);
}
});
$.ajax({
type: "DELETE",
url: "/Workout/Workout",
data: { 'id': '123456' }
success: function(msg){
alert("Data Saved: " + msg);
}
});
$.get("/Workout/Workout", { 'id': '123456' }, function(msg){
alert("Data Saved: " + msg);
});
記住創建一個登錄方法,你會發送在所有的行動需要token
,讓你知道用戶操縱你的數據是real
。