Csharp/C#教程:使用Url.Link在Web API中生成HTTPS链接分享


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

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

可以使用生成链接

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

这会返回正确的Url,但是,我需要它始终是https。 Url.Link似乎使用当前请求的方案生成URL。 例如,如果生成url的请求类似于https://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链接。

就像是:

上述就是C#学习教程:使用Url.Link在Web API中生成HTTPS链接分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!

  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" }); 

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/cdevelopment/1008349.html

(0)
上一篇 2021年12月29日
下一篇 2021年12月29日

精彩推荐