1

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

1 Answer 1

2

You could include the action name in your Api route definition:

routes.MapHttpRoute(
    name: "DefaultApi",
    routeTemplate: "api/{controller}/{action}/{id}",
    defaults: new { id = RouteParameter.Optional }
);

and then have this action:

[HttpPost]
public void Add(int id)
{

}

Now you could trigger a POST request to the /api/friends/add/123 url.

The [HttpPost] attribute ensures that this action can only be invoked using the POST verb. If you remove it you could still invoke it through GET, but that's something you shouldn't do with actions that potentially modify state on the server.

Sign up to request clarification or add additional context in comments.

1 Comment

That makes sense, but I tried it like this pastebin.com/A9nNqq26 and it aint working, it still only hits the FriendsController constructor, I have this other method that calls the Get() on the same controller that works just fine pastebin.com/XcuHzE0i

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.