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); } });