Creating a different route to a specific action mvc 6
With attribute routing you can use a tilde (~) on the Action
's route attribute to override the default route of the Controller
if needed:
[Route("api/[controller]")]public class AccountsController : Controller { [HttpPost] [Route("~/api/token")] //routes to `/api/token` public async Task<JwtToken> Token([FromBody]Credentials credentials) { ... } [HttpPost] [Route("users")] // routes to `/api/accounts/users` public async Task CreateUser([FromBody] userDto) { ... }}
For ASP.NET Core it seems that the tilde ~
symbol (see accepted answer) is not needed anymore to override the controller's route prefix – instead, the following rule applies:
Route templates applied to an action that begin with a / don't get combined with route templates applied to the controller. This example matches a set of URL paths similar to the default route.
Here is an example:
[Route("foo")]public class FooController : Controller{ [Route("bar")] // combined with "foo" to map to route "/foo/bar" public IActionResult Bar() { // ... } [Route("/hello/world")] // not combined; maps to route "/hello/world" public IActionResult HelloWorld() { } }