简体   繁体   中英

Internal Server Error 500 in MVC in Production Server

It's working fine in my local and in the Test Server. But upon deploying this in Production server.

I'm getting the Internal Server Error 500 with this function.

All the function works fine except for this one.

Function to call the sending of email in the view:

function ProcessSendingEmail(NextMRB, to, originator, owner, probDesc, hold_day, MRB_Type) 
{
       $.ajax({
              type: "POST",
              url: '@Url.Action("SendMailToFutureHold", "MRB")',
              data: { MRBNumber: NextMRB, MailTo: to, Originator: originator, Owner: owner, ProbDesc: probDesc, hold_days: hold_day, mrb_type: MRB_Type},
            success: function (data) {
                 console.log("Email Sent");
              },
              error: function (data) {
                  console.log("Email Not Sent");
              }
         });
}

Controller

[HttpPost]
public ActionResult SendMailToFutureHold(string MRBNumber, string MailTo, string Originator, string Owner, string ProbDesc, string hold_days, string mrb_type)
{
        MailId = Originator + "@domain.com";

        DataTable dt = MRB.GetDataForAttachmentForFH(MRBNumber, MailTo, mrb_type);

        byte[] bytes;
        //dt.Columns.RemoveAt(dt.Columns.Count - 1);
        dt.TableName = "MRB_Future_Hold_List_" + MRBNumber;

        Response.ClearContent();
        Response.Buffer = true;
        Response.AddHeader("content-disposition", "attachment;filename = MRB_Future_Hold_List_" + MRBNumber + ".csv");
        Response.ContentType = "text/csv";
        Response.Charset = "";

        using (StringWriter sw = new StringWriter())
        {
            int iColCount = dt.Columns.Count;
            int iRowcount = dt.Rows.Count;

            if (iRowcount != 0)
            {

                for (int i = 0; i < iColCount; i++)
                {
                    sw.Write(dt.Columns[i]);
                    if (i < iColCount - 1)
                    {
                        sw.Write(",");
                    }
                }

                sw.Write(sw.NewLine);

                foreach (DataRow dr in dt.Rows)
                {
                    for (int i = 0; i < iColCount; i++)
                    {
                        if (!Convert.IsDBNull(dr[i]))
                        {
                            sw.Write(dr[i].ToString());
                        }

                        if (i < iColCount - 1)
                        {
                            sw.Write(",");
                        }
                    }

                    sw.Write(sw.NewLine);
                }
            }
            else
            {
                sw.Write("Something went wrong. Please contact it_quality_support@domain.com");
            }

            using (HtmlTextWriter htw = new HtmlTextWriter(sw))
            {
                using (MemoryStream mStream = new MemoryStream(Encoding.ASCII.GetBytes(sw.ToString())))
                {
                    string messageBody = "<b>Originator :</b> " + Originator.ToUpper() + "<br> <b>Owner :</b> " + Owner.ToUpper() + "<br> <b>Problem Description :</b> <br>" + ProbDesc.ToUpper() + "<br><br><font> Attached file are the list of lots to put on MRB Hold. </font><br><br>";

                    bytes = mStream.ToArray();
                    Mail_Service sndmail = new Mail_Service("mailrelay.domain.com", "noreply-mrb@domain.com", MailTo, QA_Contact_Email + "," + MailId + ",it_quality_support@domain.com");
                    sndmail.Send_Mail("LOTS FOR MRB FUTURE HOLD", messageBody, bytes, MRBNumber);
                }

                Response.Flush();
                Response.End();
            }
        }

        return View();
}

If anyone can help me. It is much appreciated. Thanks.

Error Result

The issue is not with your Javascript function but with your controller method SendMailToFutureHold .

From stack-error we can read null reference exception object not set to an instance of an object

Assuming that DataTable dt = MRB.GetDataForAttachmentForFH(MRBNumber, MailTo, mrb_type); is correctly initialised( not null) -

Response might not be initialised.

The only way to be sure is to do some logging - that is the only way - I am aware - of debugging Production code.

You can try log4net

Basically this issue not related with you front-end code . It's coming from the back-end method which you consuming in Ajax call .

I Would say, use Try and Catch in the.cs method and log the error in catch. Because 500 internal server error coming due to runtime error in your code.

Create you own Logger class, you can implement the Log4net in your project and write the log in your server. In this way you can get the answer what reason causing the 500 Internal Server error in your Ajax Response

[HttpPost]
public ActionResult SendMailToFutureHold(string MRBNumber, string MailTo, string Originator, string Owner, string ProbDesc, string hold_days, string mrb_type)
{
Try
   {
         Your code just paste it here
         return View();
   }        
catch(Exception ex)
   {
         //Log the exception here with following things:- Stack Trace, Error
   }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM