问题描述
我需要生成一个绝对的URL到ASP.NET Web API,用于以后的回调/重定向。
I need to generate an absolute url to an ASP.NET Web API for a later callback/redirection.
可以使用所产生的链接
Url.Link("RouteName", new { controller = "Controller", action = "Action" });
这返回正确的网址,但我需要它永远为HTTPS。 Url.Link似乎产生使用当前请求的方案的URL。例如,如果请求产生的URL是一样的东西然后Url.Link生成一个HTTP URL。如果请求生成的URL为然后Url.Link生成一个HTTPS URL。
This returns the correct Url, however, I need it to always be https. Url.Link appears to generate a url using the scheme of the current request. For example, if the request generating the url is something like http://www.myhost.com/controller/generateUrl then Url.Link generates an http url. If the request generating the url is https://www.myhost.com/controller/generateUrl then Url.Link generates an https url.
URL需要始终使用https产生。是否有一个参数或路由值可以被传递给Url.Link实现这一目标?
The url needs to always be generated using https. Is there a parameter or route value that can be passed to Url.Link to achieve this?
感谢。
推荐答案
这似乎并不可能与 Url.Link()
我要检查的Htt prequest并将其更改为https让所有 Url.Link()
返回一个HTTPS链接。
I would check the HttpRequest and change it to https to get all Url.Link()
to return an https link.
是这样的:
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" });
这篇关于生成使用Url.Link在网页API HTTPS链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!