4

I have a Controller called QuotaController, and i can access it via httprequests, like this:

localhost:12345/quota/

What i want is to put an endpoint somewhere so i can access it like:

localhost:12345/quota/increment

or

localhost:12345/quota/decrement

How can this be done?

0

1 Answer 1

5

You could change your web api route definition to allow passing an action name:

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

And then:

public class QuotaController : ApiController
{
    public void Increment()
    {
        ...
    }

    public void Decrement()
    {
        ...
    }
}
Sign up to request clarification or add additional context in comments.

5 Comments

I already have a route like that, right now quotacontroller looks like this: public class QuotaController : ApiController { public HttpResponseMessage GetQuota(int id){} [System.Web.Http.HttpPost] public HttpResponseMessage SetQuota(int id, DocType doc, QuotaType quota, int value){} [System.Web.Http.HttpPost] public HttpResponseMessage Increment{} [System.Web.Http.HttpPost] public HttpResponseMessage Decrement{} } Can i have more than one post request in one controller?
Yes, you can have more than one POST action.
Your url seems to be missing the api prefix. Shouldn't it be localhost:15339/api/quota/increment? At least that's how it should be according to the route definition I've shown in my answer.
i just got your comment, sorry: i left the api prefix from the route, but also from the request, so it's okay like that. but the Increment and Decrement has the same parametering, and i get multiple actions were found error :/
But if you remove the api prefix how are you able to disambiguate between your API controllers and your standard controllers? Or in this ASP.NET MVC application you only have API controllers and not UI?

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.