vb.net 如何在 ASP.NET 中使用 Request.UrlReferrer?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24246937/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 17:42:27  来源:igfitidea点击:

How to use Request.UrlReferrer in ASP.NET?

asp.netvb.neturl

提问by user3724490

I have two aspx web pages. In the first one I have this code:

我有两个 aspx 网页。在第一个我有这个代码:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    Response.Redirect("~/Code.aspx")
End Sub

Now in the Code.aspx page I have this code:

现在在 Code.aspx 页面中,我有以下代码:

Label1.Text = Request.UrlReferrer.ToString

I want the label show the first page URl, but there is a runtime ERROR.How to fix this? thanks

我希望标签显示第一页 URl,但有一个运行时错误。如何解决这个问题?谢谢

This is the error message: Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

这是错误消息: Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

回答by Full Time Skeleton

Take the redirect out of the page load and put it on a link or something other than a response.redirect. Then try something like this in your code to make sure it's not null:

从页面加载中取出重定向并将其放在链接或 response.redirect 以外的其他内容上。然后在你的代码中尝试这样的事情以确保它不为空:

if(Request.UrlReferrer != null)
{ 
    Label1.Text = Request.UrlReferrer.ToString();
}
else
{
    Label1.Text = "No URL referrer";
}

Sorry just noticed you're using VB.net, the code is easy enough to change though and the theory is the same.

抱歉,刚刚注意到您正在使用 VB.net,但代码很容易更改,而且原理是相同的。

If you require the response.redirect I suppose a cookie based solution would work but seems a little involved for such a basic requirement.

如果您需要 response.redirect 我想基于 cookie 的解决方案会起作用,但似乎有点涉及这样的基本要求。