Hello friends,
I have a problem with Asp .Net core routing mechanism.
I am developing a cms project. For this reason, I want to use seo-based urls to display the pages. While doing this, of course, I need to set up a routing mechanism that will support different languages.
In addition, the default language of the site on the first boot on cms should open it. It should pull the default language from the database. If it's not defined there, it should set the browser's language by default.
The problem I'm having now is: I have defined a routing in the middleware("{culture?}/{slug?}") This routing naturally catches every request coming to the application and starts to process it. For example static javascript files, favicon file etc.. it catches all requests and tries to process them. However, I want it to capture urls similar to these only on this middleware. All incoming requests other than these and the like should be processed normally.
E.g;
site.com
site.com/about
site.com/services
site.com/contact
or
site.com/en-US/about
site.com/en-US/services
site.com/en-US/contact
What I want to do is set up a routing that evaluates and captures both requests by language and when no language code is received, through the default language, and routes it onto a single controller action. The reason why I do it on a single controller will be to query the incoming seo url information in the database and find the relevant page. So I will not keep the page ids on the url. While pulling the pages, I will query them with the seo url information.
For example, when a request like "site.com/en-US/about" comes in, I will fetch the page with the seo-url field "/about" and the language field en-US in my pages table in the database.
If I'm following the wrong path or thinking wrong, please guide me.. I need your help. What is the best and performant way to do this job?
Thank you in advance for your help.
CodePudding user response:
have you try the route attribute it's could be helpful
[Route("Home/About")]
[Route("Home/About/{id?}")]
public IActionResult About(int? id)
{
return ControllerContext.MyDisplayRouteInfo(id);
}
and I created a class to handle my route like this (try something like this pass IRouteBuilder to a class and handle it)