Reputation: 1893
In dotnet core, I'm trying to create a webapi endpoint that have a value before the controller name. ex:
template: "api/{AccountId}/[controller]"
endpoint "api/435ABC/Reports/EndOfYear"
I've seen many examples on how to do this in MVC and in Framework 4.x, but not many with WebApi and where I set a parameter before the controller name.
Upvotes: 1
Views: 2742
Reputation: 1375
In attribute routing you should change your controller route to [Route("api")]
to accept all calls from https://example.com/api
.
Note: it will affect all routes inside the Reports controller.
[Route("api")]
public class ReportsController : ApiController
and then decorate your action with route attribute like below:
[Route("{id}/[controller]/[action]")]
this way you can call your action method with https://example.com/api/435ABC/Reports/EndOfYear
.
In convention-based routing you should only add route in UseMvc method and remove Route attributes from controller and action:
app.UseMvc(routes =>
{
routes.MapRoute(name: "default", template: "api/{controller=Home}/{action=Index}"); // this line specifies default route
routes.MapRoute(name: "Reports", template: "api/{id}/{controller=Reports}/{action=EndOfYear}"); // this line specifies your custom route
});
Upvotes: 3