我正在编写一个 Express JS 应用程序,我需要计算给定控制器功能或路由路径的路由。
例如:
let myUrl = functionToGetRoutePath(ThingsController.getThing); // would return https://host/api/v1/things/{id}
// Or
let myUrl = functionToGetRoutePath('/things/{id}'); // would return https://host/api/v1/things/{id}
或者,给定参数,它可以填写 URL 参数,例如:
let myUrl = functionToGetRoutePath(ThingsController.getThing, { id: 1 }); // would return https://host/api/v1/things/1
// Or
let myUrl = functionToGetRoutePath('/things/{id}', { id: 1 }); // would return https://host/api/v1/things/1
来自 .NET,使用 UrlHelper 非常容易:
new UrlHelper(Request).Link("Things Route Name", new { id = 1 }); // returns https://host/api/v1/things/1