- Modern Web Development with ASP.NET Core 3
- Ricardo Peres
- 500字
- 2021-06-18 18:35:58
Using dynamic routing
Up until now, we've seen routing tables that statically map route templates to controller actions, but there is another kind: dynamic routes. In this case, we are still using route templates, but the thing is, we can change them dynamically.
A dynamic route handler is registered through a call to MapDynamicControllerRoute. I will provide an example that uses a translation service to translate the controller and action names supplied by the user, in any language to plain English, as they exist in the project.
Let's start from the beginning. We define the interface for the translation service:
public interface ITranslator
{
Task<string> Translate(string sourceLanguage, string term);
}
As you can see, this has a single asynchronous method, Translate, that takes two parameters: the source language and the term to translate. Let's not waste much time with this.
The core dynamic routing functionality is implemented as a class inheriting from DynamicRouteValueTransformer. Here is an example of one such class, followed by its explanation:
publicsealedclassTranslateRouteValueTransformer : DynamicRouteValueTransformer { privateconststring _languageKey ="language"; privateconststring _actionKey ="action"; privateconststring _controllerKey ="controller"; privatereadonlyITranslator _translator; publicTranslateRouteValueTransformer(ITranslatortranslator) { this._translator =translator; } publicoverride async ValueTask<RouteValueDictionary> TransformAsync(
HttpContexthttpContext, RouteValueDictionaryvalues) { varlanguage=values[_languageKey] asstring; varcontroller=values[_controllerKey] asstring; varaction=values[_actionKey] asstring; controller= await this._translator.Translate(
language, controller) ??controller; action= await this._translator.Translate(language, action)
??action; values[_controllerKey] =controller; values[_actionKey] =action; returnvalues; } }
The TranslateRouteValueTransformer class receives on its constructor an instance of ITranslator, which it saves as a local field. On the TransformAsync method, it retrieves the values for the route template values, language, controller, and action; for controller andaction, it has them translated by ITranslator. The resulting values are then stored again in the route values dictionary, which is returned in the end.
To make this work, we need three things:
- We need to register ITranslator as a service in ConfigureServices:
services.AddSingleton<ITranslator, MyTranslator>();
//MyTranslator is just for demo purposes, you need to roll out your own dictionary implementation
- We need to registerTranslateRouteValueTransformer as a service too:
services.AddSingleton<TranslateRouteValueTransformer>();
- And finally, we need to register a dynamic route:
app.UseEndpoints(endpoints=> {
endpoints.MapDynamicControllerRoute<TranslateRouteValueTransformer>(
pattern: "{language}/{controller}/{action}/{id?}");
//now adding the default route
endpoints.MapDefaultControllerRoute();
});
As you can see, our dynamic route looks for a pattern of language/controller/action/id, where the id part is optional. Any request that can be mapped to this pattern will fall into this dynamic route.
Keep in mind that the purpose of dynamic routes is not to change the route pattern, but just to change the route template tokens. This will not cause any redirect, but will actually determine how the request is to be processed, the action method and the controller, and any other route parameters.
To bring this section to a close, this example allows the resolution of these routes, provided that the dictionary supports French (fr), German (de), and Portuguese (pt):
- /fr/Maison/Index to /Home/Index
- /pt/Casa/Indice to /Home/Index
- /de/Zuhause/Index to /Home/Index
Having learned about dynamic routes, let's go back to static routes, this time using attributes in classes and methods to define the routes.
- Learning C# by Developing Games with Unity 2020
- Redis Applied Design Patterns
- 摩登創客:與智能手機和平板電腦共舞
- Java面向對象軟件開發
- 深度學習經典案例解析:基于MATLAB
- 深入淺出Prometheus:原理、應用、源碼與拓展詳解
- 從程序員到架構師:大數據量、緩存、高并發、微服務、多團隊協同等核心場景實戰
- 機械工程師Python編程:入門、實戰與進階
- Learning Selenium Testing Tools(Third Edition)
- Visual C++開發入行真功夫
- Learning Apache Cassandra
- QGIS 2 Cookbook
- Learning iOS Security
- Kotlin進階實戰
- Learning WordPress REST API