Tạo page thông báo lỗi trong asp.net website

Tạo page thông báo lỗi trong asp.net website

Khi publish một website chạy trên internet thì sẽ phát sinh nhiều lỗi ngoài mong muốn. Một lỗi thường xuyên gặp nhất là lỗi 404 (Page not found), hoặc các lỗi khác như mất kết nối database, lỗi nhập chuỗi (injection strings) mà thông qua các lỗi đó mà các hacker có thể xâm nhập vào website. 

Tạo page thông báo lỗi trong asp.net website

Đầu tiên, trong web.config bạn phải thiết lập lại <customerror> không được hiện thông báo lỗi cụ thể khi phát sinh lỗi. Khi phát sinh cụ thể thông báo lỗi, các hacker có thể lần theo đó và xâm nhập vào hệ thống. Các bạn thiết lập như sau:

 

<customErrorsmode="OndefaultRedirect="GenericErrorPage.htm">
      <errorstatusCode="403redirect="NoAccess.htm" />
      <errorstatusCode="404redirect="FileNotFound.htm" />
</customErrors>
 

Các bạn tạo file Global.asax và khai báo trong Application_error như sau:

 
void Application_Error(object sender, EventArgs e)
    {
        // Code that runs when an unhandled error occurs
        Exception ex = HttpContext.Current.Server.GetLastError();
        if (ex.InnerException != null)
        {
            ex = ex.InnerException;
        }
        if (ex is HttpException)
        {
            if (((HttpException)ex).GetHttpCode() == 404)
            {
                Response.Redirect("~/PageNotFound.htm");
            }
            else
            {
                Response.Redirect("~/PageError.html");
            }
        }
 
        HttpContext.Current.Server.ClearError();
 
    }
 

Các bạn thiết kế trang thông báo lỗi sao cho bắt mắt, sau đó dẫn link về tuỳ theo lỗi phát sinh. Đây là một vd một trang thông báo lỗi ngộ nghĩnh ^^

 

 

Bạn thấy bài viết này như thế nào?: 
Average: 9 (1 vote)
Ảnh của Tommy Tran

Tommy owner Express Magazine

Drupal Developer having 9+ year experience, implementation and having strong knowledge of technical specifications, workflow development. Ability to perform effectively and efficiently in team and individually. Always enthusiastic and interseted to study new technologies

  • Skype ID: tthanhthuy

Advertisement

 

jobsora

Dich vu khu trung tphcm

Dich vu diet chuot tphcm

Dich vu diet con trung

Quảng Cáo Bài Viết

 
CEO Mark Zuckerberg sẽ gặp gỡ ai tại Việt Nam?

CEO Mark Zuckerberg sẽ gặp gỡ ai tại Việt Nam?

Chuyến đi bất ngờ của Mark Zuckerberg đến VN chỉ vì mục đích cá nhân, không phải vì công việc.

Bối cảnh ra đời trang mạng xã hội Facebook

Con người là những tế bào xã hội. Chúng ta có mặt trên đời là hệ quả của những hành động của người khác

Recover Deleted files on Android Phone

Recover Deleted files on Android Phone

Most of the time it happens to all of us, when we accidentally delete a very important file from our Android Phone.

Công ty diệt chuột T&C

 

Diet con trung