Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Attribute Routing with Default Route

Using attribute based routing I would like to match the following routes:

~/
~/Home
~/Home/Index

to HomeController.Index, and this route:

~/Home/Error

to HomeController.Error. Here is my configuration:

[Route("[controller]/[action]")]
public class HomeController : Controller {
    [HttpGet, Route(""), Route("~/")]
    public IActionResult Index() {
        return View();
    }

    [HttpGet]
    public IActionResult Error() {
        return View();
    }
}

I have tried adding Route("[action]"), Route("Index"), and other combinations but still don't get a match for:

/Home/Index
like image 345
B Z Avatar asked Jan 26 '18 17:01

B Z


2 Answers

The empty route Route("") combines with the route on controller, use Route("/") to override it instead of combining with it:

[Route("[controller]/[action]")]
public class HomeController : Controller {

    [HttpGet]
    [Route("/")]
    [Route("/[controller]")]
    public IActionResult Index() {
        return View();
    }

    [HttpGet]
    public IActionResult Error() {
        return View();
    }
}

Alternatively, you can remove the /[action] on the controller, which makes it a bit easier (no need to override), but then you have to define a route on every action.

I'm assuming that you intentionally want to use attribute routing rather than conventional routing, so the above answer is what you need. However, just in case the assumption is wrong, this can be easily achieved with a simple conventional routing:

routes.MapRoute("default", "{controller=Home}/{action=Index}/{id?}");
like image 171
Racil Hilan Avatar answered Sep 21 '22 02:09

Racil Hilan


[Route("[controller]")]
   public class HomeController : Controller
   {
      [Route("")]     // Matches 'Home'
      [Route("Index")] // Matches 'Home/Index'
      public IActionResult Index(){}

      [Route("Error")] // Matches 'Home/Error'
      public IActionResult Error(){}

 }
like image 44
Yuli Bonner Avatar answered Sep 22 '22 02:09

Yuli Bonner