c# - 如何从 ASP.NET Core 中的路由模板生成 URL
问题描述
我有两个独立的 ASP.NET Core 站点A1
并A2
基于同一个共享库B
。
A1
并且A2
在 a1.example.com 和 a2.example.com 上,它们有带有路由模板的控制器,看起来像
A1
:
[HttpGet("/Feature1/{featureId}/EditEntry/{entryId}", Name = "EditFeature1Entry")]
public IActionResult EditFeature1Entry(long featureId, Guid entryId, string token)
{
[...]
}
在项目A1
中,我会使用
var url = Url.RouteUrl("EditFeature1Entry", new {featureId = 3, entryId = someGuid, token})
它会产生:
url = "/Feature1/3/EditEntry/someGuid?token=token"
但是要从 构建相同的 url A2
,我需要自己生成 URL,因为该路由未在项目中注册A2
(控制器在 project 中A1
)。
我想做的是将模板放在库中的某个位置B
并使用 URL 生成器来执行类似的操作
var url = Url.RouteUrlFromTemplate("/Feature1/{featureId}/EditEntry/{entryId}", new {featureId = 3, entryId = someGuid, token}, "https", "a1.example.com", null);
为了生成
url = "https://a1.example.com/Feature1/3/EditEntry/someGuid?token=token"
是否有任何预先构建的工具或者我应该自己做?我觉得我可以使用当前 Url 构建器中存在的大部分内容,但我更愿意依赖框架:)
解决方案
推荐阅读
- node.js - 使用nodejs在猫鼬中保存嵌套文档时的错误处理
- php - 正则表达式在 Ubuntu 服务器中不起作用
- c++ - 我可以在应用程序的 atexit 处理程序中使用导入的 DLL 函数吗?
- reactjs - 对 Material UI 做出反应,找不到自定义字体
- android - Giphy android sdk 没有返回发布版本的数据
- android - 为什么 Android Studio 一次又一次地显示 ERROR: Failed to open zip file。即使我正在创建一个新项目?
- c# - 检测 Visual Studio 是否正在运行
- reactjs - 如何在 ReactJs 中正确使用 shouldComponentUpdate() 和 componentWIllUpdate() 方法?
- java - Eclipse Oxygen 中的 Maven 问题
- corda - 解析量
在 Corda 外壳中