【问题标题】:Combining API controller calls and controller calls in same MVC 6 controller在同一个 MVC 6 控制器中组合 API 控制器调用和控制器调用
【发布时间】: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


【解决方案1】:

你快到了。

您的代码 sn-p 中的 AddTeam() 方法需要一个 GET 请求,因此这可能解释了您提到的 POST 不起作用的原因。但是您希望此方法响应 POST 请求而不是 GET 请求,因为它会更改数据。 GET 请求通常使用 URL 查询参数完成,以这种方式更改数据有点危险。方法签名应该是这样的:

[Route("api/Teams/{team}")]
[HttpGet("{team}", Name = "AddTeam")]
public void AddTeam([FromBody]Team item)

别忘了如果你想调用EditTeam()DeleteTeam()你必须分别发送一个PUT或DELETE请求

【讨论】:

    【解决方案2】:

    您的控制器属性中有一些错误。

    [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,但它必须转到 [HttpPost] 方法以确定您是否正在发出发布请求。

    【讨论】:

      猜你喜欢
      • 2017-10-14
      • 2015-08-24
      • 1970-01-01
      • 2018-06-18
      • 1970-01-01
      • 2012-12-16
      • 2015-09-07
      • 2018-01-24
      相关资源
      最近更新 更多