在类中使用重定向选项 [英] Use redirect option in a class

查看:82
本文介绍了在类中使用重定向选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hellow.i有一个叫做学生的课程,我有几种方法。在我创建学生的方法中,我首先检查该学生是否存在,然后如果发现我将错误发回给他。它带给我的是异常因为我使用了一个throw语句,它带来了令人讨厌的黄色死亡屏幕。我需要有人告诉我如何使用response.redirect,以便不再抛出该错误,而是将其重定向到我创建的自定义错误页面,



以下是学生班的代码



学生exists = db.Students.FirstOrDefault(x = >  x.studentName == studentName); 
if (存在!= null
{


throw new 异常( 学生存在);
}



任何我如何使用重定向到我的错误页面或如何在我的webform代码后面调用它来使用他们的行?谢谢

解决方案

你好,

你必须使用Global.asax文件,并实现Application_Error方法:

MSDN:如何:处理应用程序级错误 [ ^ ]


为此您将拥有在Global.asax文件中的Application_Error事件处理程序中编写错误处理代码。当发生任何不可处理的异常时调用它。



代码如下:



  void  Application_Error( object  sender,EventArgs e)
{
< span class =code-comment> // 发生未处理错误时运行的代码

// 重定向到自定义错误页面,如果需要,使用查询字符串或其他一些机制将错误代码和错误消息传递给自定义错误页面。
Response.Redirect( CustomErrorPage.aspx);

}


试试这样



学生班修改这样的方法



  public   bool  IsStudentExist( string  studentName)
{
学生存在= db.Students.FirstOrDefault(x = > x.studentName == studentName);
返回存在!= null ;

}





在您打电话的页面中,检查学生是否存在,

喜欢这个



学生obj = 学生(); 
bool isexist = obj.IsStudentExist( studendName);
if (isexist)
Response.Redirect( Error.aspx?Message = Student Exists);





创建错误页面如下



< pre lang =xml> < html xmlns = http://www.w3 .org / 1999 / xhtml >
< head runat = server >
< 标题 > < / title >
< / head >
< body >
< 表单 id = form1 runat = 服务器 >
< div >
< h3 >
< asp:Label ID = lblMessage runat = server > < / asp:标签 >
< / h3 >
< / div >
< / form >
< / body >
< / html >





  protected   void  Page_Load( object  sender,EventArgs e)
{
lblMessage.Text = Request.QueryString [ 消息];
}


Hellow.i have a class Called Students where i have several methods.In my method for creating students,i first check if that student exists then i throw error back to him if found.What it brings back to me is the Exception since i have used a throw statement and it brings that annoying YELLOW SCREEN OF DEATH .I need someone to tell me how i can use a response.redirect so as instead of throwing that error,it redirects him to a custom error page i have created,

Here is the code on the Students Class

Student exist = db.Students.FirstOrDefault(x => x.studentName == studentName);
           if (exist != null)
           {


               throw new Exception("Student Exists");
           }


Any how i can use a redirect to my error page or how i can call it on my webform code behind to use the line their?Thanks

解决方案

Hello,
You have to use a Global.asax file, and implement the Application_Error method :
MSDN : How to: Handle Application-Level Errors[^]


For this you will have to write error handling code in Application_Error event handler in the Global.asax file. It is called when any unhandle exception occurs.

Code as follows:

void Application_Error(object sender, EventArgs e)
{
    // Code that runs when an unhandled error occurs

    //Redirect to custom error page and if required pass error code and error message to the custom error page using query string or some other mechenism.
    Response.Redirect("CustomErrorPage.aspx");

}


try like this

in the Student class modify the method like this

public bool IsStudentExist(string studentName)
    {
        Student exist = db.Students.FirstOrDefault(x => x.studentName == studentName);
        return exist != null;

    }



And in the page where you are calling that method to check the student exists ,
like this

Student obj = new Student();
       bool isexist = obj.IsStudentExist("studendName");
       if (isexist)
           Response.Redirect("Error.aspx?Message=Student Exists");



Create an Error page as follows

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h3>
            <asp:Label ID="lblMessage" runat="server"></asp:Label>
        </h3>
    </div>
    </form>
</body>
</html>



protected void Page_Load(object sender, EventArgs e)
        {
            lblMessage.Text = Request.QueryString["Message"];
        }


这篇关于在类中使用重定向选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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