在以前版本的 asp.net 中,我们可以使用
@Request.Url.AbsoluteUri
但是好像变了。我们如何在 asp.net core 1.0 中做到这一点?
您必须分别获取主机和路径。
@Context.Request.Host
@Context.Request.Path
您需要方案、主机、路径和查询字符串
@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}")
您可以使用 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 的解决方案是最好的(可能包含在您自己的扩展方法中)。
这在带有 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。
使用 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。
您可以考虑使用此扩展方法(来自 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¶m2=b
Request.GetUri(addQuery: false);
//after >> https://localhost:44304/information/about
//before >> https://localhost:44304/information/about?param1=a¶m2=b
new Uri("https://localhost:44304/information/about?param1=a¶m2=b").GetHostWithNoSlash();
//after >> https://localhost:44304
接受的答案对我有帮助,@padigan 的评论也对我有帮助,但如果你想像我一样包含查询字符串参数,那么试试这个:
@Context.Request.PathBase@Context.Request.GetEncodedPathAndQuery()
您需要在视图中添加 @using Microsoft.AspNetCore.Http.Extensions
才能使 GetEncodedPathAndQuery() 方法可用。
如果您还希望从请求中获取端口号,则需要通过 AspNet Core 的 Request.Host
属性访问它。
Request.Host
属性不仅仅是一个字符串,而是一个包含主机域 和 端口号的对象。如果在 URL 中明确写出端口号(即 "https://example.com:8080/path/to/resource"
),则调用 Request.Host
将为您提供主机域和端口号,如下所示:"example.com:8080"
。
如果您只需要主机域的值或只需要端口号的值,那么您可以单独访问这些属性(即 Request.Host.Host
或 Request.Host.Port
)。
有一种从 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 元标记。
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
var returnUrl = string.IsNullOrEmpty(Context.Request.Path) ? "~/" : $"~{Context.Request.Path.Value}{Context.Request.QueryString}";
不定期副业成功案例分享
@HttpContext
而不是@Context
。对于部分视图@Context
有效。我忘记使用了吗?Url.Action("Action", null, null, Request.Url.Scheme);
stackoverflow.com/questions/434604/…Request.QueryString
添加到末尾,因为 Request.Path 不包含查询字符串