Forum Discussion

0belix's avatar
0belix
Copper Contributor
Jan 07, 2022

How to localize Routes in Core 5 MVC and/or Razor Pages

Hi,

 

I've been developing websites and applications in webforms for the last 15 years.

With the appearance of URL Routing, i've been using it for SEO purposes, allowing multilingual websites to change the URLs according to the user selected language, while calling the same route.

This allows me to have localized routes call the same webform. Ex:

mydomain.com/{language}/{category_meta_id}/{product_meta_id} 

  • mydomain.com/en/games/my-game
  • mydomain.com/pt/jogos/o-meu-jogo

the above route can call the same webform, for instance ProductDetail.aspx, and have it gather the parameter 'language' to decide the language to load, 'category_meta_id' to understand the area to call that must be challenged on the database along with the selected language, and the 'procuct_meta_id' that also uses the language to challenge the database in order to determine which content to load.

This is supported by a DB table structure like:

 

Category

-CategoryId (PK)

-IsActive

 

CategoryText

-CategoryTextId (PK)

-CategoryId (FK)

-Language

-MetaId

-Title

-ContentText

 

Product
-ProductId (PK)

-CategoryId (FK)

-ProductImage

-IsActive

 

ProductText

-ProductTextId (PK)

-ProductId (FK)

-Language

-MetaId

-Title

-ContentText

 

Bottom line is, i can have a localized URL call the same exact product on the selected language while using one single form to handle the request.

 

I've been considering making the transition to either Core5 MVC or Core5 Razor Pages, but due to the way both these technologies handle routing, i'm not sure how, or even if it is possible to have the same URL approach without having to either duplicate folders in case of the Razor Pages, or duplicate controllers and views in the case of MVC. 

 

Making this transition also seems to implicate loosing the capacity of determining all the URL parts from the database, which currently allows content admins to create categories and/or products on the fly without having to code new Controllers and Views for each language of a new category or product.

 

For instance, if a content admin wishes to add a new product with the title 'Game 2' in English and 'Jogo 2' in Portuguese, he/she only needs to use a form on the backend to create the new product, first in one language, then on the other under the same ProductId, allowing that the URLs mydomain.com/en/games/game-2 and mydomain/pt/jogos/jogo-2 to call the same ProductDetail.aspx webform. The same could be done to create a new category, for instance 'Toys' / 'Brinquedos'.

 

How can i achieve the above on MVC or Razor pages? It seems to me that the content admin would have to create new controllers and views, or folders and pages in the case of Razor Pages, for each product or category while also having to duplicate them for each language... am I missing something? Can someone give me a hand on this?

 

Thanks

 

 

2 Replies

  • 0belix's avatar
    0belix
    Copper Contributor
    no one? really?!?! is there no solution, or was i just too dense explaining the problem?!?!
    • cl7382's avatar
      cl7382
      Copper Contributor
      I followed this sample and was able to implement localization in my ASP.NET Core 5 Razor Pages app. Good luck! https://www.mikesdotnetting.com/article/345/localisation-in-asp-net-core-razor-pages-cultures

Resources