1

I have two controllers for my User model. One is a regular MVC controller which handles the views and inherits from Controller. Then I have an API controller which inherits from ApiController. Their names are UsersController and UsersApiController respectively. I wish to remove from "Api" from the API controller's URL so I can type:

www.mywebsite.com/api/Users

rather than:

www.mywebsite.com/api/UsersApi

which contains a redundant "api".

I have tried applying the attribute [Route("Users")] and [Route("api/Users")] to the entire controller but neither work and they seem to just break it. My WebApiConfig.cs has the default route config.

How can I do this?

1 Answer 1

4

You should be able to use Attribute routing.

[RoutePrefix("api/users")]
public class UsersApiController : ApiController
{  
    [Route("")]
    public HttpResponseMessage Get() 
    { 

    }
}

You should be able to customize it per action methods as well

public class UsersApiController : ApiController
{  
    [Route("api/users")]
    public HttpResponseMessage Get() 
    { 

    }
}
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.