Is it possible to have a parameter in the middle of the URL for a Razor Page?
Example:
File Structure:
I can create the route for the products list, but can't figure out the parameter in the middle of the URL using options.Conventions.AddPageRoute
Thanks
You can add a convention and use parameters inside the same way as in controller routes.
options.Conventions.AddPageRoute("/Products", "products/{id}/details")
options.Conventions.AddPageRoute("/Products", "products/{id}/listofaccesories")
or in a more general way
options.Conventions.AddPageRoute("/Products", "products/{id}/{handler}")
The {handler} parameter will be used by the razor pages engine to find a method mapping this route.
This two configurations will route the url /products/20120/details to the ProductsModel page, taking the 20120 as the id argument and selecting the Details handler inside the ProductsModel.
public ProductsModel : PageModel
{
...
public Task OnGetDetails(int id)
{
...
}
public Task OnGetListOfAccessories(int id)
{
...
}
...
}
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