重定向时出现错误 [英] I Get an error when Redirecting
问题描述
你好,
我有一个名为Redirector.cs
的类
公共 class 重定向器:IRedirector
{
公共 无效 GoToAccountRegisterPage()
{
重定向(" );
}
私有 无效重定向(字符串路径)
{
HttpContext.Current.Response.Redirect(path);
}
}
这是Redirector.cs的界面.
public interface IRedirector
{
无效 GoToAccountRegisterPage();
}
这是另一个Web表单..Go.aspx
单击链接后,它应该转到另一页.该链接的类为
GoToRegister()
这是我的CS代码
公共 类 LoginPresenter
{
私有 IRedirector _ redirector = null;
公共 无效 GoToRegister()
{
_ redirector . GoToAccountRegisterPage();
}
}
但是当我单击网页上的链接时,出现这样的错误
对象引用未设置为对象的实例.
异常详细信息:System.NullReferenceException:对象引用未设置为对象的实例.
源错误:
Line 31: public void GoToRegister() Line 32: { Line 33: _redirector.GoToAccountRegisterPage(); Line 34: } Line 35:
有人可以告诉我为什么吗..我可以做什么!!
谢谢.
调用GoToRegister
方法时,_redirector
仍然为null.这就是为什么您会得到例外.
您应该在代码的适当部分设置_redirector
的值,并在GoToRegister
方法中使用_redirector
之前应检查_redirector
是否为null.
>
Hello,
I have a class called Redirector.cs
public class Redirector : IRedirector
{
public void GoToAccountRegisterPage()
{
Redirect("~/Account/Register.aspx");
}
private void Redirect(string path)
{
HttpContext.Current.Response.Redirect(path);
}
}
This is my interface for Redirector.cs
public interface IRedirector
{
void GoToAccountRegisterPage();
}
This an another web form..Go.aspx
Once you click a Link it should go to another page..the link''s class is
GoToRegister()
this is my cs code
public class LoginPresenter
{
private IRedirector _redirector = null;
public void GoToRegister()
{
_redirector.GoToAccountRegisterPage();
}
}
But wen i click the link on the web page i get an error like this
Object reference not set to an instance of an object.
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
Source Error:
Line 31: public void GoToRegister() Line 32: { Line 33: _redirector.GoToAccountRegisterPage(); Line 34: } Line 35:
Can someone tell me why is this.. and wat can i do!!
Thank you.
When theGoToRegister
method is called, the_redirector
is still null . That''s why you get the exception.
You should set the value of the_redirector
(in proper part of your code) and in theGoToRegister
method you should check if_redirector
is null or not before using it.
这篇关于重定向时出现错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!