ChatGPT解决这个技术问题 Extra ChatGPT

如何在 asp.net core 1.0 中查看当前 url

在以前版本的 asp.net 中,我们可以使用

@Request.Url.AbsoluteUri

但是好像变了。我们如何在 asp.net core 1.0 中做到这一点?

@DavidG 看来您现在可以使用 UriHelper.GetDisplayUrl(Re‌quest)

A
Arun Prasad E S

您必须分别获取主机和路径。

 @Context.Request.Host
 @Context.Request.Path

如果您的应用程序位于虚拟目录中,这将不会报告正确的 url。如果有 Request.PathBase 值,您需要在 Request.Path 值前面加上 Request.PathBase 值。这也适用于这个问题的几乎所有其他答案。
@MohammedNoureldin:这是剃刀视图语法。
对于剃须刀页面,我必须使用 @HttpContext 而不是 @Context。对于部分视图 @Context 有效。我忘记使用了吗?
MVC 中有一个简单的方法 Url.Action("Action", null, null, Request.Url.Scheme); stackoverflow.com/questions/434604/…
您需要将 Request.QueryString 添加到末尾,因为 Request.Path 不包含查询字符串
t
tmg

您需要方案、主机、路径和查询字符串

@string.Format("{0}://{1}{2}{3}", Context.Request.Scheme, Context.Request.Host, Context.Request.Path, Context.Request.QueryString)

或使用新的 C#6 功能“字符串插值”

@($"{Context.Request.Scheme}://{Context.Request.Host}{Context.Request.Path}{Context.Request.QueryString}")

你如何获得片段,比如说test.com/abc#123,你如何从请求中获得#123 片段?
@fanray你不能。浏览器不会向服务器发送 URI 片段
考虑使用 UriBuilder 构建 URL。
A
AlexBar

您可以使用 Request 的扩展方法:

Request.GetDisplayUrl()

如果您添加 @using Microsoft.AspNetCore.Http.Extensions 然后 @Context.Request.GetDisplayUrl() 这有效
仍然得到 - 当前上下文中不存在请求
请记住,此函数的智能感知摘要说 Suitable only for display. This format should not be used in HTTP headers or other HTTP operations. 基于此,我认为@tmg 的解决方案是最好的(可能包含在您自己的扩展方法中)。
如果您希望它在与以前相同类型的 Url 对象中,您可以在获得显示 url 后执行此操作: var uri = new Uri(displayUrl);
m
mcNux

这在带有 Microsoft.AspNetCore.Http.Extensions 的 .net core 1.0 中显然总是可行的,它向 HttpRequest 添加了扩展以获得完整的 URL; GetEncodedUrl

例如从剃刀的角度来看:

@using Microsoft.AspNetCore.Http.Extensions
...
<a href="@Context.Request.GetEncodedUrl()">Link to myself</a>

从 2.0 开始,还有相对路径和查询 GetEncodedPathAndQuery


感谢您包含@using 声明
D
Dhanuka777

使用 Uri 的 AbsoluteUri 属性,使用 .Net 核心,您必须像这样从请求构建 Uri,

 var location = new Uri($"{Request.Scheme}://{Request.Host}{Request.Path}{Request.QueryString}");

 var url = location.AbsoluteUri;

例如,如果请求 url 是“http://www.contoso.com/catalog/shownew.htm?date=today”,这将返回相同的 url。


像这样的事情让我觉得 .Net Core 在某些方面是倒退了一步。这是否比我无法想象的 Asp.Net 中的 Request.Url 更好?
S
Sam Alekseev

您可以考虑使用此扩展方法(来自 Microsoft.AspNetCore.Http.Extensions 命名空间:

@Context.Request.GetDisplayUrl()

对于我的一些项目,我更喜欢更灵活的解决方案。有两种扩展方法。

1) 第一个方法从传入的请求数据创建 Uri 对象(通过可选参数具有一些变体)。 2) 第二种方法接收 Uri 对象并以以下格式返回 string(没有尾部斜杠):Scheme_Host_Port

public static Uri GetUri(this HttpRequest request, bool addPath = true, bool addQuery = true)
    {
        var uriBuilder = new UriBuilder
        {
            Scheme = request.Scheme,
            Host = request.Host.Host,
            Port = request.Host.Port.GetValueOrDefault(80),
            Path = addPath ? request.Path.ToString() : default(string),
            Query = addQuery ? request.QueryString.ToString() : default(string)
        };
        return uriBuilder.Uri;
    }

    public static string HostWithNoSlash(this Uri uri)
    {
        return uri.GetComponents(UriComponents.SchemeAndServer, UriFormat.UriEscaped);
    }

用法:

//before >> https://localhost:44304/information/about?param1=a&param2=b
        Request.GetUri(addQuery: false);
        //after >> https://localhost:44304/information/about

        //before >> https://localhost:44304/information/about?param1=a&param2=b
        new Uri("https://localhost:44304/information/about?param1=a&param2=b").GetHostWithNoSlash();
        //after >> https://localhost:44304

C
Christopher King

接受的答案对我有帮助,@padigan 的评论也对我有帮助,但如果你想像我一样包含查询字符串参数,那么试试这个:

@Context.Request.PathBase@Context.Request.GetEncodedPathAndQuery()

您需要在视图中添加 @using Microsoft.AspNetCore.Http.Extensions 才能使 GetEncodedPathAndQuery() 方法可用。


H
Hallmanac

如果您还希望从请求中获取端口号,则需要通过 AspNet Core 的 Request.Host 属性访问它。

Request.Host 属性不仅仅是一个字符串,而是一个包含主机域 端口号的对象。如果在 URL 中明确写出端口号(即 "https://example.com:8080/path/to/resource"),则调用 Request.Host 将为您提供主机域和端口号,如下所示:"example.com:8080"

如果您只需要主机域的值或只需要端口号的值,那么您可以单独访问这些属性(即 Request.Host.HostRequest.Host.Port)。


E
Emran Hussain

有一种从 Razor 页面或 PageModel 类获取当前 URL 的简洁方法。那是:

Url.PageLink()

请注意,我的意思是“ASP.NET Core Razor Pages”,而不是 MVC。

当我想在 ASP.NET Core razor 页面中打印规范的 URL 元标记时,我使用此方法。但是有一个问题!它会为您提供应该是该页面正确 URL 的 URL。让我解释。

比如说,您已经为您的页面定义了一个名为“id”的路由,因此,您的 URL 应该如下所示

http://example.com/product?id=34

Url.PageLink() 将为您提供如上所示的准确 URL。

现在,如果用户在该 URL 上添加任何额外内容,例如,

http://example.com/product?id=34&somethingElse

然后,您将不会从该方法中获得“其他东西”。这就是为什么它非常适合在 HTML 页面中打印规范的 URL 元标记。


S
Sergey
public string BuildAbsolute(PathString path, QueryString query = default(QueryString), FragmentString fragment = default(FragmentString))
{
    var rq = httpContext.Request;
    return Microsoft.AspNetCore.Http.Extensions.UriHelper.BuildAbsolute(rq.Scheme, rq.Host, rq.PathBase, path, query, fragment);
}

但是你从哪里得到 httpContext ?这不适用于 Microsoft.AspNetCore.Http.HttpContext.Request
H
Husam Ebish
var returnUrl = string.IsNullOrEmpty(Context.Request.Path) ? "~/" : $"~{Context.Request.Path.Value}{Context.Request.QueryString}";