0

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?

3
  • Try using [Route(Companies)] on your class, and then just don't include the Route attribute on the method, i.e. just [HttpGet] Commented Mar 11, 2022 at 9:33
  • Can you show us how you mapped your routes inside RegisterRoutes? Commented Mar 11, 2022 at 9:34
  • In Global.asax.cs, I use config.MapHttpAttributeRoutes(); Commented Mar 11, 2022 at 10:32

1 Answer 1

0

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

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

Comments

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.