本文介绍了如何将null coockie值存储到字符串变量中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想尝试在下面但是抛出错误

I am trying to like below but throwing an error "

Object reference not set to an instance of an object.





"

string roll = Request.Cookies["StudentCookies"].Value;
            if (roll == null || roll == String.Empty)
            {
                Response.Redirect("~/LoginPage.aspx");
            }

推荐答案

string roll = Request.Cookies["StudentCookies"] == null ? "" : Request.Cookies["StudentCookies"].Value;


string roll = string.Empty;
// first check that cookie exists, if it does, use the value
if (Request.Cookies["StudentCookies"] != null) {
    roll = Request.Cookies["StudentCookies"].Value;
}

' if cookie wasn't present or the value is empty, redirect
if ( roll == string.empty)
    Response.Redirect("~/LoginPage.aspx");


Good luck.


这篇关于如何将null coockie值存储到字符串变量中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 23:04