As you cant use Put and Delete on most hosted sites I am trying to create a route that avoids using these, but I cant get that to work..
I want a route like this
api/someController/Add/someInt
with this RESTsharp code
private RestClient client;
public RESTful()
{
client = new RestClient
{
CookieContainer = new CookieContainer(),
BaseUrl = "http://localhost:6564/api/",
//BaseUrl = "http://localhost:21688/api/",
//BaseUrl = "http://madsskipper.dk/api/"
};
}
public void AddFriend(int userId)
{
client.Authenticator = GetAuth();
RestRequest request = new RestRequest(Method.POST)
{
RequestFormat = DataFormat.Json,
Resource = "Friends/Add/{userId}"
};
request.AddParameter("userId", userId);
client.PostAsync(request, (response, ds) =>
{
});
}
To hit this method in my FriendsController
// POST /api/friends/add/Id
[HttpPost] //Is this necesary?
public void Add(int id)
{
}
So I have added this in my route config
routes.MapHttpRoute(
name: "ApiAdd",
routeTemplate: "api/{controller}/Add/{id}",
defaults: new { id = RouteParameter.Optional }
);
But when I do this I only hit my FriensController's Constructor and not the Add Method
EDIT:
Also tried making this route config
routes.MapHttpRoute(
name: "ApiAdd",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { action = "Add", id = RouteParameter.Optional }
);
But same result, the controller is hit but not the action
Solution: Found out the parameters was added wrongly with RESTsharp, so instead of
RestRequest request = new RestRequest(Method.POST)
{
RequestFormat = DataFormat.Json,
Resource = "Friends/Add/{userId}"
};
request.AddParameter("userId", userId);
It should be
RestRequest request = new RestRequest(Method.POST)
{
RequestFormat = DataFormat.Json,
Resource = "Friends/Add/{userId}"
};
request.AddUrlSegment("userId", userId.ToString());