Che*_*hev 354 c# asp.net asp.net-mvc razor
我在我正在建立的博客上使用Facebook评论插件.它有一些FBXML标记,由页面上引用的facebook javascript解释.
这一切都很好,但我必须将当前的,完全限定的URL传递给插件.
<div style="width: 900px; margin: auto;">
<div id="fb-root"></div>
<fb:comments href="URL HERE" num_posts="10" width="900"></fb:comments>
</div>
Run Code Online (Sandbox Code Playgroud)
获取当前页面的URL的最佳方法是什么?请求URL.
这是我的解决方案的最终代码:
<fb:comments href="@Request.Url.AbsoluteUri" num_posts="15" width="900"></fb:comments>
Run Code Online (Sandbox Code Playgroud)
Dar*_*rov 522
您可以使用Request.RawUrl
,Request.Url.OriginalString
,Request.Url.ToString()
或Request.Url.AbsoluteUri
.
And*_*tan 48
将此扩展方法添加到您的代码中:
public static Uri UrlOriginal(this HttpRequestBase request)
{
string hostHeader = request.Headers["host"];
return new Uri(string.Format("{0}://{1}{2}",
request.Url.Scheme,
hostHeader,
request.RawUrl));
}
Run Code Online (Sandbox Code Playgroud)
然后你可以在RequestContext.HttpContext.Request
酒店外执行它.
Asp.Net中存在一个错误(可以是侧面步骤,见下文),该错误发生在使用本地网站端口80以外的端口的计算机上(如果内部网站通过虚拟IP上的负载平衡发布,则会出现一个大问题)并且端口在内部用于发布规则),即Asp.Net将始终在AbsoluteUri
属性上添加端口- 即使原始请求不使用它.
此代码确保返回的url始终等于浏览器最初请求的Url (包括端口 - 因为它将包含在主机头中),然后再进行任何负载平衡等操作.
至少,它确实在我们(相当复杂!)的环境中:)
如果介于两者之间的任何时髦代理重写主机头,那么这也无效.
2013年7月30日更新
正如@KevinJones在下面的评论中提到的那样 - 我在下一节中提到的设置已在此处记录:http://msdn.microsoft.com/en-us/library/hh975440.aspx
虽然我不得不说当我尝试它时我无法正常工作 - 但这可能只是我制作了一个错字或其他东西.
2012年7月9日更新
我不久前遇到过这个问题,并且想要更新这个答案,但从未这样做过.当一个upvote刚刚进入这个答案我认为我现在应该这样做.
我在Asp.Net中提到的'bug'可以通过一个明显未记录的appSettings值来控制 - 称为'aspnet:UseHostHeaderForRequest'
- 即:
<appSettings>
<add key="aspnet:UseHostHeaderForRequest" value="true" />
</appSettings>
Run Code Online (Sandbox Code Playgroud)
我HttpRequest.Url
在ILSpy中查看时遇到了这一点 - 由--->
ILSpy视图中的以下复制/粘贴左侧指示:
public Uri Url
{
get
{
if (this._url == null && this._wr != null)
{
string text = this.QueryStringText;
if (!string.IsNullOrEmpty(text))
{
text = "?" + HttpEncoder.CollapsePercentUFromStringInternal(text,
this.QueryStringEncoding);
}
---> if (AppSettings.UseHostHeaderForRequestUrl)
{
string knownRequestHeader = this._wr.GetKnownRequestHeader(28);
try
{
if (!string.IsNullOrEmpty(knownRequestHeader))
{
this._url = new Uri(string.Concat(new string[]
{
this._wr.GetProtocol(),
"://",
knownRequestHeader,
this.Path,
text
}));
}
}
catch (UriFormatException)
{ }
}
if (this._url == null) { /* build from server name and port */
...
Run Code Online (Sandbox Code Playgroud)
我个人没有使用它 - 它没有文档,因此不能保证坚持下去 - 但它可能会做我上面提到的相同的事情.为了提高搜索结果的相关性 - 并承认其他人已经发现了这一点 - Nick Aceves在Twitter上也提到了这一'aspnet:UseHostHeaderForRequest'
设置
Bri*_*den 14
public static string GetCurrentWebsiteRoot()
{
return HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority);
}
Run Code Online (Sandbox Code Playgroud)
我也是出于Facebook的原因寻找这个,到目前为止所给出的答案都没有按照需要或太复杂.
@Request.Url.GetLeftPart(UriPartial.Path)
Run Code Online (Sandbox Code Playgroud)
获取完整的协议,主机和路径"没有"查询字符串.如果您使用的是默认值80以外的其他内容,还包括端口.
我最喜欢的...
Url.Content(Request.Url.PathAndQuery)
Run Code Online (Sandbox Code Playgroud)
要不就...
Url.Action()
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
387784 次 |
最近记录: |