在页面加载时获取异常 [英] getting exception at page load

查看:69
本文介绍了在页面加载时获取异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当未登录的用户尝试访问用户的主页时,页面加载时出现条件检查异常.


因为该用户的会话不存在,所以它总是捕获异常..

什么是在页面加载时检查会话条件的正确方法?


这是下面的代码..


i am getting exception with condition checking at page load when the user who is not logged in try to access user''s home page


because the session of that user does not exists so it always catch exception..

what should be the right way to check session condition at page load??


here is the code below..


try
        {
            if (Session["user_type"].ToString() == "User")
            {
                if (!IsPostBack)
                {
                    prpobj.userid = Convert.ToInt32(Session["user_id"]);
                    DataTable dtbl = mainobj.viewprofile(prpobj);
                    if (dtbl.Rows[0]["image_thumbpath"].ToString() != string.Empty)
                    {
                        Image.ImageUrl = dtbl.Rows[0]["image_thumbpath"].ToString();
                    }
                    else
                    {
                        Image.ImageUrl = "~/Images/img_default.jpg";
                    }
                    lblusername.Text = dtbl.Rows[0]["first_name"].ToString() + " " + dtbl.Rows[0]["last_name"].ToString();
                }
            }
            else if (Session["user_type"].ToString() == "Admin")
            {
                Response.Redirect("~/Access_Denied1.aspx");
            }
            else
            {
                Response.Redirect("~/Signin.aspx");
            }
        }
        catch (Exception)
        {
            Response.Redirect("~/Signin.aspx");
        }



注意-这个人多次编辑了他的问题,以对我的辱骂取代了这个问题,因为我给了他正确的答案.去搞清楚.我想如果您也想受到虐待,您可以尝试重复我的答案,因为它是唯一正确的答案,然后看看他是否也对您一视同仁.



Note - this guy has edited his question several times to replace it with abuse towards me, because I gave him the right answer. Go figure. I guess if you want to be abused too, you could try to repeat my answer, given that it''s the only correct one, and see if he treats you the same.

推荐答案

steeve_richard写道:
steeve_richard wrote:

if(Session ["user_type"].ToString()=="User")

if (Session["user_type"].ToString() == "User")



这在很多层面上都被打破了.您应该购买一本基本的C#书籍并阅读,因为这看起来非常基础.



This is broken on so many levels. You should buy a basic C# book and read it, because the very basics appear to be lost on you.

if (Session["user_type"] != null



您需要检查以确保其不为空.

另一件事是,您应该使用具有静态公共字符串的类来一遍又一遍地替换键入user_type.如果有人将User_Type放在某个地方,usre_type或其他错字怎么办?该代码将编译,但行为不可预测.就个人而言,我会让用户键入一个枚举,而不是一组字符串,这也会导致它们被强类型键入并且不是随机的.



is what you need to check to make sure it''s not null.

The other thing is, you should use a class that has static public strings to replace typing user_type over and over. What if someone puts User_Type in somewhere, or usre_type, or some other typo ? The code will compile but behave unpredictable. Personally, I''d make the user types an enum, not a set of strings, which also causes them to be strongly typed and not random.


我删除了您的帖子,不发布如果您有话要说,请假冒的答案"来编辑您的帖子.在这种情况下,您没有.你说:

I deleted your post, do NOT post fake ''answers'' edit your post if you have something useful to say. In this case, you did not. You said:

i do not want to buy a book...  
why do not you tell me?



好吧,如果您不想买书,那么您肯定不该从事任何商业项目,如果您只是出于娱乐目的而编写安全的登录系统,则仍然应该买书,或者至少要读书.

但是,我直接告诉您,我确实告诉您什么地方出了问题,给了您可以复制和粘贴的代码,并就代码为何如此糟糕给出了其他一些一般性建议.你怎么会错过呢?这就是为什么您需要一本书的原因,当它就在您面前时,您对它的了解还不够.



Well, if you don''t want to buy a book, then you sure as hell should not be working on any commercial projects, if you''re writing a secure login system just for fun, you should still buy a book, or at least do some reading.

Nevertheless, I DID tell you, I told you exactly what is wrong, gave you code you could copy and paste, and gave you some other general advice as to why your code is terrible as is. How could you miss it ? This is why you need a book, you don''t know enough to know the answer when it''s right in front of you.


这篇关于在页面加载时获取异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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