c# - 如何获取 MVC Controller 中当前的 url 协议(protocol)?

标签 c# asp.net-core-mvc

此方法返回域和端口:

public Uri BuildUrl()
{
    Uri domain = new Uri(Request.GetDisplayUrl());
    return new Uri(domain.Host + (domain.IsDefaultPort ? "" : ":" + domain.Port));
}

但它不包含“http://”或“https://”部分。我怎样才能得到它?

最佳答案

您需要从 UriScheme 属性获取协议(protocol)。

public Uri BuildUrl()
{
    Uri domain = new Uri(Request.GetDisplayUrl());
    return new Uri(domain.Scheme + "://" + domain.Host + (domain.IsDefaultPort ? "" : ":" + domain.Port));
}

关于c# - 如何获取 MVC Controller 中当前的 url 协议(protocol)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59595994/

相关文章:

c# - EntityFramework 和延迟加载

c# - 更改代码段中文字的顺序

c# - 使用 Blazor 将输入文本动态绑定(bind)到类/对象属性

带有 nginx 设置主机头的 ASP.Net Core 不起作用

c# - 如何在 MSBuild 14.0 中重新加载 Microsoft.Build.Evaluation.Project

c# - 如何从 Controller 获取到 Identity/Pages/Account/ConfirmEmail 的回调 URL?

c# - 异常处理中间件和页面

asp.net-core - ASP.NET 5 : How to configure the path of logs in Microsoft. 扩展.日志记录

asp.net-core-mvc - EF7 和 GroupBy() 无法翻译

c# - 如何更改 C# 文本文件中的未知数字?