开发者

ViewData["s"].ToString() vs. Request.QueryString["s"].ToString(), what is returned if "s" was never set?

开发者 https://www.devze.com 2023-01-10 19:07 出处:网络
If in my controller: public ActionResult Index() { //no code implied return View; } Then in the view that is returned:

If in my controller:

public ActionResult Index()
{
     //no code implied
     return View;
}

Then in the view that is returned:

<%if(ViewData["SomeString"].ToString() != "True") {%> show this <%}%>

I will get an error at runtime because of an object reference having no object.

However inside of a page where I do:

<%if(Request.QueryString["Something"].ToString() != "True") {%> show this <%}%>

Update: I actually do get the error.

Edit开发者_如何学JAVA: Looks like they act the same after all.


Both ViewData and QueryString will return null for non-existent key. When you're trying to call a method (in your case, ToString) on a null object reference, you get a NullReferenceException.

I'm not sure what's not clear in this situation.

0

精彩评论

暂无评论...
验证码 换一张
取 消