Sam February 2016

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();
   }

}

Answers


Sam February 2016

Looks like I needed to add the controller token in there

public class AccountsController : Controller
{

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

}


Tseng February 2016

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();
   }
}

Post Status

Asked in February 2016
Viewed 3,731 times
Voted 10
Answered 2 times

Search




Leave an answer