如何在asp.net core 1.0中获取当前url

时间:2016-07-18 12:41:01

标签: asp.net-core asp.net-core-mvc asp.net-core-1.0

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

@Request.Url.AbsoluteUri

但它似乎已经改变了。我们怎么能在asp.net core 1.0中做到这一点?

11 个答案:

答案 0 :(得分:86)

您需要scheme,host,path和queryString

@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}")

答案 1 :(得分:85)

您必须单独获取主机和路径。

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

答案 2 :(得分:61)

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

Request.GetDisplayUrl()

答案 3 :(得分:10)

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

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

 var url = location.AbsoluteUri;

e.g。如果请求网址是' http://www.contoso.com/catalog/shownew.htm?date=today'这将返回相同的URL。

答案 4 :(得分:6)

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);
}

答案 5 :(得分:5)

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

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

如果您只想要主机域的值或只想要端口号的值,那么您可以单独访问这些属性(例如"example.com:8080"Request.Host.Host)。

答案 6 :(得分:5)

在带有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

答案 7 :(得分:2)

有一种干净的方法可以从Razor页面或PageModel类获取当前URL。那是:

Url.PageLink()

请注意,我的意思是“ ASP.NET Core 剃刀页”,而不是MVC。

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

说,您已经为页面定义了一个名为“ id”的路由,因此,您的网址应类似于

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

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

现在,如果用户在该URL上添加了其他内容,例如

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

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

答案 8 :(得分:1)

您可以考虑使用此扩展方法(来自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

答案 9 :(得分:0)

被接受的答案对我很有帮助,@ padigan对此的评论也是如此,但是如果您希望像我这样包括查询字符串参数,请尝试以下操作:

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

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

答案 10 :(得分:0)

$req = new Request([$request]);
$req['email']=$acc->email;
$req['password'] = $request->password;