1

I have an authenticate function in my company controller

    [ActionName("Authenticate")]
    [HttpGet]
    public bool Authenticate(Company company)
    {
        if (Uow.Companies.AuthenticateCompany(company))            
            return true;                
        return false;
    }

that is called using the following ajax query

$.ajax({ url: "/api/company/Authenticate", type: 'get', data: company })

company is a js object

Company: function (name, phoneNumber, password) {
    this.Name = name;
    this.PhoneNumber = phoneNumber;
    this.password = password;
}

var company = new Company($('#TextBoxCompanyName').val(),'00000000', $('#TextBoxCompanyPassword').val());

and my api route is as follows

    config.Routes.MapHttpRoute(
        name: "Action",
        routeTemplate: "api/{controller}/{action}"
    );

When the code runs the web api calls the following function in the company controller

 public Company Get(int id)
    {
        return Uow.Companies.GetById(id);
    }

How do I call a custom get function?

3
  • What is company in the JS ($.ajax) code? Commented Jan 28, 2013 at 20:44
  • @carlosfigueira made an update Commented Jan 28, 2013 at 20:48
  • 1
    The order of the routes matters. If you have multiple routes then the "Action" should comes first then the default one. Commented Jan 28, 2013 at 20:52

1 Answer 1

4

Web.API tries to match the routes in the definition order.

From Web API Routing and Actions/Routing and Action Selection

The framework tries to match the segments in the URI path to the template. Literals in the template must match exactly. A placeholder matches any value, unless you specify constraints. The framework does not match other parts of the URI, such as the host name or the query parameters. The framework selects the first route in the route table that matches the URI.

So the order of definition of the routes matter.

You need to put your "Action" before the "DefaultApi"

config.Routes.MapHttpRoute(
        name: "Action",
        routeTemplate: "api/{controller}/{action}"
    );

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

Otherwise the wep.api interprets your url /api/company/Authenticate as controller=company and id=Authenticate so it routes to your Get action.

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

2 Comments

Thanks, The company data is not being sent through. Do you have any idea why?
In the case of GET request and complex types like Company you need to annotate your parameter with FromUriAttribute : public bool Authenticate([FromUri]Company company) { more info: blogs.msdn.com/b/jmstall/archive/2012/04/16/…

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.