首页 > 解决方案 > 如何从 ASP.NET Core 中的路由模板生成 URL

问题描述

我有两个独立的 ASP.NET Core 站点A1A2基于同一个共享库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 构建器中存在的大部分内容,但我更愿意依赖框架:)

标签: c#asp.net-coreasp.net-core-routing

解决方案



推荐阅读