使用Url.Link在Web API中生成HTTPS链接

我需要为ASP.NET Web API生成绝对URL以用于以后的回调/重定向。

可以使用生成链接

Url.Link("RouteName", new { controller = "Controller", action = "Action" }); 

这会返回正确的Url,但是,我需要它始终是https。 Url.Link似乎使用当前请求的方案生成URL。 例如,如果生成url的请求类似于http://www.myhost.com/controller/generateUrl,则Url.Link会生成一个http url。 如果生成URL的请求是https://www.myhost.com/controller/generateUrl,则Url.Link会生成httpsurl。

需要始终使用https生成url。 是否有可以传递给Url.Link的参数或路由值来实现此目的?

谢谢。

Url.Link()似乎不可能

我会检查HttpRequest并将其更改为https以使所有Url.Link()返回https链接。

就像是:

  if (Url.Request.RequestUri.Scheme != Uri.UriSchemeHttps) { var secureUrlBuilder = new UriBuilder(Url.Request.RequestUri); secureUrlBuilder.Scheme = Uri.UriSchemeHttps; Url.Request.RequestUri = new Uri(secureUrlBuilder.ToString()); } // should now return https Url.Link("RouteName", new { controller = "Controller", action = "Action" });