Combining API controller calls and controller calls in one MVC 6 controller

I am just starting with MVC 6, having previously created separate controllers for API calls and standard controller calls. There is no APIController class in MVC 6, and these actions can be included in your Controller class.

So, I have TeamController. I have an action to return a view:

[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) 

I am not sure if I am set up correctly, for example, when I make a message in Javascript, GET is called instead of GET, and when I call Delete, GetByTeamId is called instead.

Can someone please give advice on how best to configure these routes?

EDIT: here is the 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); } }); 
+5
source share
2 answers

You are almost there.

The AddTeam() method, as in your code snippet, expects a GET request, so this probably explains why the POST you talked about doesn't work. But you would like this method to respond to a POST request, not a GET request, since it modifies the data. GET requests are usually executed with URL request parameters, and it is a little dangerous to modify the data in this way. The method signature should look like this:

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

And don't forget, if you want to call EditTeam() or DeleteTeam() , you must send a PUT or DELETE request, respectively

+1
source

You have some errors in your controller attributes.

 [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) 

no verb and route needed. Verb overload uses a route. I'm not sure about your Javascript POST, but it should go to the [HttpPost] method if you are making a mail request.

0
source

All Articles