HI WELCOME TO KANSIRIS

ASP.NET MVC Application_Error handler in Global.asax

Leave a Comment
Global.asax
protected void Application_Error(object sender, EventArgs e)
    {
        Exception exception = Server.GetLastError();
        Response.Clear();

        if (exception != null)
        {
            Common.WriteErrorLog(exception);
        }

        HttpException httpException = exception as HttpException;
        if (httpException != null)
        {
            RouteData routeData = new RouteData();
            routeData.Values.Add("controller", "Error");
            switch (httpException.GetHttpCode())
            {
                case 404:
                    // page not found
                    routeData.Values.Add("action", "HttpError404");
                    break;
                case 500:
                    // server error
                    routeData.Values.Add("action", "HttpError500");
                    break;
                default:
                    routeData.Values.Add("action", "Index");
                    break;
            }
            routeData.Values.Add("error", exception.Message);
            // clear error on server
            Server.ClearError();

            Response.RedirectToRoute(routeData.Values);
            // at this point how to properly pass route data to error controller?
            //Response.Redirect(String.Format("~/Error/{0}/?message={1}", "Index", exception.Message));
        }
    }
Controller :
public class ErrorController : Controller
{
    // GET: Error
    public ActionResult Index(string error="")
    {
        ViewBag.Message = error;
        return View();
    }

    public ActionResult HttpError404(string error = "")
    {
        ViewBag.Message = error;
        return View();
    }

    public ActionResult HttpError500(string error = "")
    {
        ViewBag.Message = error;
        return View();
    }
}
to get emails when error oocured
protected void Application_Error(object sender, EventArgs e) { Exception exception = Server.GetLastError(); Response.Clear(); HttpException httpException = exception as HttpException; string txt = Convert.ToString(httpException); if (httpException != null) { string action; string email = "xxxx@xxxxx.com"; switch (httpException.GetHttpCode()) { case 404: // page not found action = " Error occured in application HttpError404"; break; case 500: // server error action = "Error occured in application HttpError500"; break; default: action = "Error occured in application General"; break; } EmailSendingUtility EmailSend = new EmailSendingUtility(); EmailSend.Email_maaaahwanam(email,txt , action); // clear error on server Server.ClearError(); // Response.Redirect(String.Format("~/Error/{0}/?message={1}", action, exception.Message)); // return Content("<script language='javascript' type='text/javascript'>alert('User Record Not Available');location.href='" + @Url.Action("Index", "NUserRegistration") + "'</script>"); Response.Write("<script language='javascript'>window.alert('Some thing went wrong please try again after some time click OK to continue!!!');window.location='/nhomepage/index';</script>"); } }

using System;
using System.Collections.Generic;
using System.Linq;
//using System.Net.Mail;
using System.Net;
using System.Net.Mail;
//using System.Web.Mail;
using System.Text;
using System.Threading.Tasks;
namespace MaaAahwanam.Utility
{
    public class EmailSendingUtility
    {
        public void Email_maaaahwanam(string txtto, string txtmessage, string subj)
        {
            MailMessage Msg = new MailMessage();
            Msg.From = new MailAddress("xxxxxxxx@gmail.com");
            Msg.To.Add(txtto);
            //ExbDetails ex = new ExbDetails();
            Msg.Body = txtmessage;
            Msg.Subject = subj;
            Msg.IsBodyHtml = true;
            // your remote SMTP server IP.
            SmtpClient smtp = new SmtpClient();
            smtp.Host = ("smtp.gmail.com").ToString();
            System.Net.NetworkCredential NetworkCred = new System.Net.NetworkCredential();
            NetworkCred.UserName = ("xxxxxx@gmail.com").ToString();
            NetworkCred.Password = ("xxxxx").ToString();
            smtp.UseDefaultCredentials = true;
            smtp.Credentials = NetworkCred;
            smtp.Port = 587;
            smtp.EnableSsl = true;
            smtp.Send(Msg);
            //Mail method for go daddy
            //string HostAdd = "relay-hosting.secureserver.net";
            //string FromEmailid = "ixxxx@xxx";
            //string Pass = "spreadinghappiness";
            //string to = txtto.ToString();
            //MailMessage mailMessage = new MailMessage();
            //mailMessage.From = FromEmailid;
            //mailMessage.Subject = subj;
            //mailMessage.BodyFormat = MailFormat.Html;
            //mailMessage.Body = txtmessage;
            //mailMessage.To = to;
            //System.Web.Mail.SmtpMail.SmtpServer = HostAdd;
            //System.Web.Mail.SmtpMail.Send(mailMessage);
            //mailMessage = null;
        }
    }

0 comments:

Post a Comment

Note: only a member of this blog may post a comment.