Search code examples
asp.net-mvc-routingasp.net-coreattributeroutingasp.net-core-1.0

Attribute Routing in ASP.NET Core 1.0


Do I need to configure anything to use attribute routing in an ASP.NET Core 1.0 application?

The following doesn't seem to be working for me. I was expecting to hit this method when I go to localhost:132/accounts/welcome

public class AccountsController : Controller
{

   [Route("welcome")]
   public IActionResult DoSomething()
   {
       return View();
   }

}

Solution

  • An alternative you can use is to apply a RoutePrefix or Route on your class. Then you won't have to repeat that part on the action attributes.

    [Route("[controller]")]
    public class AccountsController : Controller
    {
       [Route("welcome")]
       public IActionResult DoSomething()
       {
           return View();
       }
    }