Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Razor Pages ASP.Net Core 2.0 Routing - Is possible to have a parameters in the middle of a URL?

Is it possible to have a parameter in the middle of the URL for a Razor Page?

Example:

  • http://website/products <--- Shows a list of products
  • http://website/products/{productName} - Product Page
  • http://website/products/{productName}/Page1
  • http://website/products/{productName}/Page2
  • http://website/products/{productName}/ListOfAccessories
  • http://website/products/{productName}/ListOfAccessories/Details

File Structure:

  • /products/Index <--- Shows the list of products
  • /Products/Product/Index <-- Product Page
  • /Products/Product/Page1
  • /Products/Product/Page2
  • /Products/Product/ListOfAccessories/Index
  • /Products/Product/ListOfAccessories/Details

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

like image 326
user2585080 Avatar asked Dec 09 '25 17:12

user2585080


1 Answers

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)
    {
       ...
    }

   ...
}
like image 87
animalito maquina Avatar answered Dec 11 '25 19:12

animalito maquina