我刚开始学习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 post:
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);
}
});