UrlReferrer提供对象引用错误 [英] UrlReferrer giving object reference error

查看:112
本文介绍了UrlReferrer提供对象引用错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在尝试获取外部网站的urlreferrer.
但我收到错误消息对象引用未设置为对象的实例"

如果(!IsPostBack)
{
字符串MyReferrer = Request.UrlReferrer.ToString();
lblReferrer.Text = MyReferrer.ToString();
}

请帮帮我

感谢

hi ,

I am trying to get the urlreferrer of the external site.
But i am getting error ''Object reference not set to an instance of object''

if (!IsPostBack)
{
string MyReferrer = Request.UrlReferrer.ToString();
lblReferrer.Text = MyReferrer.ToString();
}

Please help me

Thanks

推荐答案

显然,UrlReferrer可以为null,这总是会抛出您观察到的确切异常.当用户单击带有页面URL的锚点时,浏览器会为您提供引荐数据,但是如果用户只是在地址行中键入其URL,该怎么办?引荐来源网址为null,因此您需要将其考虑在内.

此外,你在做什么?!当然,HttpRequest.UrlReferrerSystem.Uri,因此您可以使用ToString,但是您的MyReferrer已经是字符串,为什么要尝试计算字符串的ToString?请参阅:
http://msdn.microsoft.com/en-us/library/system. web.httprequest.urlreferrer.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/system.uri.aspx [ ^ ].

—SA
Apparently, UrlReferrer can be null, which would always throw the exact exception you observe. The browser gives you the referral data when the user clicks the anchor with the URL of your page, but what if the user simply type its URL in the address line? The referrer would be null, so you need to take it into account.

Besides, what are you doing?! Of course, HttpRequest.UrlReferrer is System.Uri, so you can use ToString, but your MyReferrer is already the string, why are you trying to calculate ToString of a string? Please see:
http://msdn.microsoft.com/en-us/library/system.web.httprequest.urlreferrer.aspx[^],
http://msdn.microsoft.com/en-us/library/system.uri.aspx[^].

—SA


这篇关于UrlReferrer提供对象引用错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆