I'm using ASP.Net MVC Core 2.0 and having a hard time getting routing working for hyphenated friendly URLs. I would like the following URL style:
admin is the area:
/admin/invoice-categories/new - Creates a new invoice category
/admin/invoice-categories/edit/1 - Edits a invoice category
/admin/invoice-categories - Shows the invoice categories list
This is my controller code:
[Area("admin")]
[Route("[area]/invoice-categories/"]
class InvoiceCategoriesController {
public IActionResult New() {}
public IActionResult Edit(int id) {}
public IActionResult Index() {}
}
However, this results in an exception stating the "New, Edit, Index" methods in the controller are ambiguous.
Is there a way to get it to work WITHOUT using defining the action name specifically (like HttpGet("New"))?
Unfortunately there isn't a concept of default action in MVC Core attribute routing. Your alternatives are:
Create the route in the middleware where it is possible to specify a default action:
routes.MapRoute(
name: "invoice-categories",
template: "{area}/invoice-categories/{action=Index}/{id?}",
defaults: new { area = "admin", controller = "InvoiceCategories" });
Add an extra route on your Index
action:
[Area("admin")]
[Route("[area]/invoice-categories/[action]"]
class InvoiceCategoriesController {
public IActionResult New() {}
public IActionResult Edit(int id) {}
[Route("[area]/invoice-categories"]
public IActionResult Index() {}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With