Anders Lindén
Anders Lindén

Reputation: 7293

Route that automatically gets name from webapi action

I have ApiController that looks like

[RoutePrefix("Companies")]
public class CompanyController : ApiController
{
  [HttpGet]
  [Route("GetCompanyProfile")]
  public GetProfileOutput GetCompanyProfile()
  {
     // some code
  }
}

Now, if I try to simplify the code so that I dont have to specify the route name explicitely, but instead reuse the action function name, it looks like this:

[RoutePrefix("Companies")]
public class CompanyController : ApiController
{
  [Route, HttpGet]
  public GetProfileOutput GetCompanyProfile()
  {
     // some code
  }
}

But now, the GetCompanyProfile action is not any longer recognized.

What is my code actually doing? Is there a way to have the route name automatically fetched from the action name?

Upvotes: 0

Views: 444

Answers (1)

Serge
Serge

Reputation: 43860

try this

[Route("Companies/[action]")]
public class CompanyController : ApiController
{
   public GetProfileOutput GetCompanyProfile()
  {
     // some code
  }
}

but if it is possible change controller name to CompaniesController. In this case you will not need an attribute routing

Upvotes: 0

Related Questions