Dynamic Query String Parameters in Razor Pages

Vlachopoulos Dimitris 1 Reputation point
2022-07-29T11:43:52.113+00:00

Hello colleagues,

Recently, I have decided to develop my own CMS platform because I have already written a lot of code to my clients and I said "Why don't get all this code into one project?" and that's it. OK first I understand the reasons that you will say "Why you choose to create your own CMS and don't use something like NopCommerce or something" the answers in this question is because I like to make something custom in order to support it more easily and to learn some more to develop.

To begin with my problem, I want dynamic routes! I have already implement the below code to my project.
226108-screenshot-1.png

However, the problem here is if I have more than parameters what can I do? But it is not fixed length. For instance, lets imagine we have the below menu:

  • Home
  • Categories
    • Cars
      • Audi
      • Toyota
      • Volvo
    • Bikes
      • Yamaha
      • Honda
      • Kawasaki
  • About
  • Contact

The Urls will be

  • /home
  • /categories
  • /categories/cars
  • /categories/cars/audi
  • /categories/cars/toyota
  • /categories/cars/volvo
  • /categories/bikes
  • /categories/bikes/yamaha
  • /categories/bikes/honda
  • /categories/bikes/kawasaki
  • /about
  • /contact

In above list every single page have a description which I get it from DB that's why I want to get or all query string and I will search it in DB or something else that I don't know. For example cars it is a page but in case of audi , audi will be page. It is simple if I know, I will have only three depth of menu but I don't want this because the depth of menu will be almost infinity if my client needs.

Hope you all understand what exactly I want. I look forward for your suggestions!!!

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,211 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,312 questions
{count} votes

4 answers

Sort by: Most helpful
  1. Bruce (SqlWork.com) 57,166 Reputation points
    2022-07-29T14:53:48.46+00:00

    Typically with a CMS system a limited number of actual coded routes handle the requests. For example /categories/* would go to the same handler. The paths might become parameters.

    You will need to decide on a template engine. Unless the site will be small, razor doesn’t work because I each razor template must be compiled to code and loaded.

    You will also need to decide how to store the user managed pages. Will you use a database, files or cloud storage.

    0 comments No comments

  2. Rijwan Ansari 746 Reputation points MVP
    2022-07-31T13:21:52.617+00:00
    0 comments No comments

  3. satya karki 986 Reputation points MVP
    2022-07-31T13:25:02.003+00:00
    0 comments No comments

  4. Vlachopoulos Dimitris 1 Reputation point
    2022-08-01T12:29:32.613+00:00

    First of all thanks for your replies. I have already checked all of them but unfortunately none of them fits in my problem (I think). Most of the answers were for .NET Framework and MVC but my problem is for Razor Page .ΝΕΤ 5.

    Let's change a little bit the issue. I want something to get the Query String or get the path. On Razor Page but also on MVC Controller you must pre-define the route. But as I said my route is dynamic. I want a page on /Category/Cars and I want a Page on /Category/Cars/Audi.

    The problem here is when the request comes to PageModel e.g. DynamicPageModel.cs on class HttpContext.Request.Path is a part of it. If the DynamicPageModel accepts only one parameter everything else is ignored by framework...

    I request to DynamicPage with this query string {url}/category/page/audi/service and on HttpContext.Request.Path comes only the "category".