Do i have to list all controller functions?
Do i have to list all controller functions in Global.asax.cs
file?
Im creating a api for my workout and have created the controller WorkoutController. It has the action AddWorkout that takes some parameters, for instance username, password, duration and type. Two first are string, two last is ints.
Now, do i need to create a route for it? And every action with different action signature? Why could it not fall in under the default route? Calling it would break if i dont supply the correct variables, b开发者_如何学Cut i know what im doing :D
routes.MapRoute(
"AddWorkout", // Route name
"Workout/AddWorkout/", // URL with parameters
new { controller = "Workout", action = "AddWorkout" } // Parameter defaults
);
??? :D ???
You can easily create a REST Api:
routes.MapRoute(
"Workout", // Route name
"{controller}/{action}", // URL with parameters
new { controller = "Home", action = "Index" } // Parameter defaults
);
and use for example of your Workout:
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);
}
}
But I would suggest you to use WCF though :)
From a client side:
$.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 );
});
remember to create a Login method that you would send a token
that is required in all actions, so you know that the user manipulating your data is real
.
Read this.
The "Url" property on the Route class defines the Url matching rule that should be used to evaluate if a route rule applies to a particular incoming request.
No, you don't have to do this at all. The default route looks at what methods are defined and will figure out what to call as long as your parameters line up to some method on the controller.
精彩评论