【发布时间】:2015-08-24 09:40:34
【问题描述】:
我刚开始使用 MVC 6,之前为 API 调用和标准控制器调用创建了单独的控制器。在 MVC 6 中不再有 APIController 类,这些操作可以包含在您的 Controller 类中。
所以这里我有一个 TeamsController。我有一个返回视图的操作:
[Route("Teams")]
public ActionResult Teams()
And then I have actions to return data :
//GET : api/Teams
[Route("api/Teams")]
[HttpGet("GetAllTeams")]
public IEnumerable<Team> GetAllTeams()
//GET : api/Teams/5
[Route("api/Teams/{teamId:int}")]
[HttpGet("{teamId:int}", Name = "GetTeamById")]
public IActionResult GetTeamById(int teamId)
//GET : api/Teams/Chicago Bears
[Route("api/Teams/{teamName}")]
[HttpGet("{teamName}", Name = "GetTeamByName")]
public IActionResult GetTeamByName(string teamName)
//POST : api/Teams
[Route("api/Teams/{team}")]
[HttpPost("{team}", Name = "AddTeam")]
public void AddTeam([FromBody]Team item)
//PUT: api/Teams
[Route("api/Teams/{team}")]
[HttpPut("{team}", Name = "EditTeam")]
public void EditTeam([FromBody]Team item)
//DELETE : api/Teams/4
[Route("api/Teams/{teamId:int}")]
[HttpDelete("{teamId:int}", Name="DeleteTeam")]
public IActionResult DeleteTeam(int id)
我不确定我是否正确配置了这些,例如,当我在 Javascript 中发帖时,调用的是 GET 而不是 POST,而当我调用 Delete 方法而不是调用 GetByTeamId 时。
有人可以就如何最好地设置这些路线提供建议吗?
编辑:这是 Javascript 帖子:
var tAdd = new team(self.Id(), self.TeamName(), self.Logo());
var dataObjectAdd = ko.toJSON(tAdd);
$.ajax({
url: 'http://lovelyjubblymvc6.azurewebsites.net/api/Teams',
type: 'post',
data: dataObjectAdd,
contentType: 'application/json',
success: function (data) {
self.teams.push(new team(data.TeamId, data.TeamName, data.Logo));
self.TeamName('');
self.Logo('');
},
error: function (err) {
console.log(err);
}
});
【问题讨论】:
-
你能用 JavaScript 给我们看这篇文章吗?
标签: c# asp.net-core asp.net-core-mvc