I don't do Asp.Net but i'll guess that you need to specify the HttpPost
attribute as can be seen in Attribute Routing / HTTP Methods documentation :
[HttpPost]
[Route("api/Departments/PostDepartment/{accountid}/{name}/{dbContext=03}")]
public void PostDepartment(string accountid, string name, string dbContext)
{
_deptsRepository.PostDepartment(accountid, name, dbContext);
}
Small sample that work on my PC :
TestController.cs:
using System.Web.Http;
namespace WebApplication2.Controllers
{
public class TestController : ApiController
{
[HttpPost]
[Route("api/Departments/PostDepartment/{accountid}/{name}/{dbContext=03}")]
public string PostDepartment(string accountid, string name, string dbContext)
{
return accountid + name + dbContext;
}
}
}
Test.html :
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-2.0.3.min.js"></script>
<script>
$(function () {
$.ajax("api/Departments/PostDepartment/accountid/name/dbContext", {
type: 'POST', success: function (data) {
$('#dest').text(data);
}
});
});
</script>
</head>
<body>
<div id="dest"></div>
</body>
</html>
Sample program to call the service in C# :
namespace ConsoleApplication1
{
using System;
using System.IO;
using System.Net;
using Newtonsoft.Json;
class Program
{
static void Main()
{
Console.WriteLine(GetRestData(@"http://localhost:52833//api/Departments/PostDepartment/42/76TrombonesLedTheZeppelin"));
Console.ReadLine();
}
private static dynamic GetRestData(string uri)
{
var webRequest = (HttpWebRequest)WebRequest.Create(uri);
webRequest.Method = "POST";
webRequest.ContentLength = 0;
var webResponse = (HttpWebResponse)webRequest.GetResponse();
var reader = new StreamReader(webResponse.GetResponseStream());
string s = reader.ReadToEnd();
return JsonConvert.DeserializeObject<dynamic>(s);
}
}
}