在MVC 6控制器中将API控制器调用与控制器调用相结合

5

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

你能用JavaScript展示一下这篇文章吗? - Henk Mollema
2个回答

1
你即将完成。 AddTeam() 方法在你的代码片段中期望一个 GET 请求,这可能解释了为什么你提到的 POST 没有起作用。但是你希望这个方法响应 POST 请求而不是 GET 请求,因为它会改变数据。使用 URL 查询参数通常使用 GET 请求,这样更改数据有点危险。方法签名应该像这样:
[Route("api/Teams/{team}")]
[HttpGet("{team}", Name = "AddTeam")]
public void AddTeam([FromBody]Team item)

不要忘记,如果您想调用EditTeam()DeleteTeam()函数,必须分别发送PUT或DELETE请求


0

你的控制器属性中存在一些错误。

[Route("Teams")] 
public ActionResult Teams()

And then I have actions to return data :

//GET : api/Teams
[HttpGet("api/Teams")]
public IEnumerable<Team> GetAllTeams()

//GET : api/Teams/5
[HttpGet("api/Teams/{teamId:int}")]
public IActionResult GetTeamById(int teamId)

//GET : api/Teams/Chicago Bears
[HttpGet("api/Teams/{teamName}")]
public IActionResult GetTeamByName(string teamName)

//POST : api/Teams
[HttpPost("api/Teams/{team}")]
public void AddTeam([FromBody]Team item)

//PUT: api/Teams
[HttpPut("api/Teams/{team}")]
public void EditTeam([FromBody]Team item)

//DELETE : api/Teams/4
[HttpDelete("api/Teams/{teamId:int}")]
public IActionResult DeleteTeam(int id)

不需要指定动词和路由。动词重载使用路由。我不确定你的POST JavaScript,但如果你正在进行一个POST请求,它肯定要进入[HttpPost]方法。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接