Page.Request中的NullreferenceException [英] NullreferenceException in Page.Request

查看:91
本文介绍了Page.Request中的NullreferenceException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Page.Request中获得NullreferenceException

Got NullreferenceException in Page.Request in

protected Page_Load(object sender, EventArgs e) {
   ...
   route = Page.Request["routeID"].ToString();
}



这里应该创建什么对象来避免这个问题。谢谢。


What object should be created here to avoid the problem. Thanks.

推荐答案

Page.Request.Item返回一个字符串(如果存在) - 之后不需要调用.ToString()。但要注意 - http://msdn.microsoft .com / en-us / library / system.web.httprequest.item%28v = vs.110%29.aspx [ ^ ]说:

如果找不到指定的键,然后返回null。

这就是你的问题:routeID不在参数中。你检查过IsPostBack吗?
Page.Request.Item returns a string (if present) - there is no need to call .ToString() afterwards. But beware - http://msdn.microsoft.com/en-us/library/system.web.httprequest.item%28v=vs.110%29.aspx[^] says:
"If the specified key is not found, then null is returned."
And exactly that is your problem: routeID is not among the parameters. Did you check for IsPostBack?


这篇关于Page.Request中的NullreferenceException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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