如何将null coockie值存储到字符串变量中? [英] How to store null coockie value into string variable?

查看:58
本文介绍了如何将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");
            }

推荐答案

尝试:

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


您正在尝试使用Request.Cookies.Value而不检查它存在cookie。



这是正确的检查:

You are trying to use Request.Cookies.Value without checking it the cookie exists.

This is the correct check:
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值存储到字符串变量中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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