MVC C # custom MvcRouteHandler - How to do this?

Does anyone have experience in providing custom MvcRouteHandler? In my application, I would like to implement a globalization pattern, for example http: // mydomain / en / about or http: // mydomain / de / about.

As for persistence, I would like the cookie to be read as soon as the request arrives, and if there is a language setting in this cookie (for example, a user arriving at http: // mydomain / will be transferred to http: // mydomain / en / for example). If there is no cookie present, I would like to get the first language that the browser supports, apply it and save it in this cookie.

I assume that this cannot be done using a standard routing mechanism. mvc provides the initial project template in it. In the newsgroup, I got a hint to take a look at MvcRouteHandler and implement my own. But its hard to find a model of how to do this.

Any ideas?

+4
source share
2 answers

I don’t think that what you do requires a special route handler.

For your "globalized" URIs, a regular MVC route will be executed with the restriction so that the parameter "locale" is equal to "en", "de", etc. The restriction will prevent non-global URIs from matching with the route.

For a non-global URI, create an all-all route that simply redirects to the URI of the local or set cookie.

Place the “globalized” route above the “all-all” route in Global.asax so that the “already globalized” URIs do not fall into the redirection.

You will need to create a new route handler if you want a specific URI pattern to trigger something that is not an action on the controller. But I do not think that you are dealing here.

+2
source

You should be able to do this with the default ASP.NET MVC template, I am doing something similar. Just create your routes as {language} / {controller} / {action} / {id}

Just set a default route that goes to the controller, which checks the language cookie and redirects the user based on this cookie.

0
source

Source: https://habr.com/ru/post/1276815/


All Articles