one*_*end 89 asp.net-core-mvc asp.net-core asp.net-core-1.0
在以前的asp.net版本中,我们可以使用
@Request.Url.AbsoluteUri
Run Code Online (Sandbox Code Playgroud)
但它似乎已经改变了.我们怎样才能在asp.net core 1.0中做到这一点?
Cli*_*t B 102
您必须单独获取主机和路径.
@Context.Request.Host@Context.Request.Path
Run Code Online (Sandbox Code Playgroud)
tmg*_*tmg 97
您需要scheme,host,path和queryString
@string.Format("{0}://{1}{2}{3}", Context.Request.Scheme, Context.Request.Host, Context.Request.Path, Context.Request.QueryString)
Run Code Online (Sandbox Code Playgroud)
或使用新的C#6功能"字符串插值"
@($"{Context.Request.Scheme}://{Context.Request.Host}{Context.Request.Path}{Context.Request.QueryString}")
Run Code Online (Sandbox Code Playgroud)
小智 70
您可以使用以下扩展方法Request:
Request.GetDisplayUrl()
Run Code Online (Sandbox Code Playgroud)
Dha*_*777 14
使用Uri 的AbsoluteUri属性,你必须使用.Net核心从这样的请求构建Uri,
var location = new Uri($"{Request.Scheme}://{Request.Host}{Request.Path}{Request.QueryString}");
var url = location.AbsoluteUri;
Run Code Online (Sandbox Code Playgroud)
例如,如果请求网址为" http://www.contoso.com/catalog/shownew.htm?date=today ",则返回相同的网址.
use*_*616 10
您可以考虑使用此扩展方法(来自Microsoft.AspNetCore.Http.Extensions命名空间:
@Context.Request.GetDisplayUrl()
Run Code Online (Sandbox Code Playgroud)
对于我的一些项目,我更喜欢更灵活的解决方案。有两种扩展方法。
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);
}
Run Code Online (Sandbox Code Playgroud)
用法:
//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
Run Code Online (Sandbox Code Playgroud)
显然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>
Run Code Online (Sandbox Code Playgroud)
从2.0开始,还具有相对路径并查询GetEncodedPathAndQuery。
接受的答案对我有帮助,@padigan 对它的评论也对我有帮助,但是如果您想像我一样包含查询字符串参数,请尝试以下操作:
@Context.Request.PathBase@Context.Request.GetEncodedPathAndQuery()
Run Code Online (Sandbox Code Playgroud)
并且您需要@using Microsoft.AspNetCore.Http.Extensions在视图中添加 ,以便 GetEncodedPathAndQuery() 方法可用。
小智 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);
}
Run Code Online (Sandbox Code Playgroud)
如果您还希望从请求中获取端口号,则需要通过Request.HostAspNet Core 的属性访问它.
该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()
Run Code Online (Sandbox Code Playgroud)
请注意,我的意思是“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 元标记。
| 归档时间: |
|
| 查看次数: |
88691 次 |
| 最近记录: |