Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

}
like image 275
Sam Avatar asked Feb 08 '23 19:02

Sam


2 Answers

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();
   }
}
like image 118
Tseng Avatar answered Feb 12 '23 13:02

Tseng


Looks like I needed to add the controller token in there

public class AccountsController : Controller
{

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

}
like image 29
Sam Avatar answered Feb 12 '23 12:02

Sam