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

 
Tool tăng tốc DNS, cải thiện 40% tốc độ duyệt web

Tool tăng tốc DNS, cải thiện 40% tốc độ duyệt web

Nhóm nghiên cứu thuộc trường đại học Northwestern University giới thiệu công cụ giúp giải quyết điểm yếu của các dịch vụ DNS công cộng.

Facebook, Twitter, Social Netwok, Social Media, Face.com

Tài khoản Facebook và Twitter có thể bị khai thác qua Face.com

Ứng dụng di động KLIK của Face.com đã bị khóa sau khi một nhà nghiên cứu phát hiện ra rằng nó có thể được sử dụng để khai thác tài khoản Facebook và Twitter.

Sử dụng Bootswatch Themes ở Drupal 7 như thế nào?

Sử dụng Bootswatch Themes ở Drupal 7 như thế nào?

The standard look and feel of Bootstrap is unmistakable and often you can spot a website using it a mile away. The dead giveaways are the buttons and navigation, but it doesn't have to be this way.

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

 

Diet con trung