gpt4 book ai didi

c# - 为什么 Request.Url.Scheme 不返回 HTTPS?

转载 作者:太空狗 更新时间:2023-10-29 21:33:40 26 4
gpt4 key购买 nike

我在本地服务器上工作,我需要通过 HTTPS 访问特定的 URL,而其余的则通过 HTTP。我已将 Visual Studio 配置为使用 IIS Express,因此我可以使用 HTTP/SSL。

我有这样一个方法:

[RequireHttps]
public ActionResult SomeHttpsMethod()
{
//Do something
}

在另一个地方我有:

var url = Url.Action("SomeHttpsMethod", "SomeHttpsController", new { someParams }, Request.Url.Scheme);

如果我使用 HTTP 访问我的站点,即 http://localhost:httpport,我仍然会从 Request 返回 HTTP .Url.Scheme 而不是 HTTPS。这就是它的工作方式吗?

显然,如果我使用 HTTPS 访问我的站点,即以 https://localhost:sslport 开头,返回 HTTPS(是我想要的)但我不想只针对特定的 URL/ Controller 方法以 HTTPS 访问该站点。

最佳答案

这一行:

var url = Url.Action("SomeHttpsMethod", "SomeHttpsController", 
new { someParams }, Request.Url.Scheme);

根据您的当前 请求的方案(即 HTTP)构造一个 URL。 Request 总是指当前请求。

你最好在这个地方硬编码“https”,因为无论如何你总是希望它是安全的:

var url = Url.Action("SomeHttpsMethod", "SomeHttpsController", 
new { someParams }, "https");

关于c# - 为什么 Request.Url.Scheme 不返回 HTTPS?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25667898/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com