如何在.NET 8中获取请求主机?

问题描述 投票:0回答:1

在 .NET 4.5.2 中,此代码适用于

_layout.cshtml
:

@{
    string s = Request.Url.AbsoluteUri;
}

但是在 .NET 8 项目中的同一位置,代码会导致错误:

名称“request”在当前上下文中不存在

我应该用什么来代替?

c# .net-core .net-8.0
1个回答
0
投票

您可以尝试使用

RazorPage.Context
:

@{
    string s = Context.Request.GetDisplayUrl();
}

RazorPageBase.ViewContext

@{
    string s = ViewContext.HttpContext.Request.GetDisplayUrl();
}
© www.soinside.com 2019 - 2024. All rights reserved.