【问题标题】:Internal Server Error 500 in MVC in Production Server生产服务器中的 MVC 中的内部服务器错误 500
【发布时间】:2020-07-21 06:16:30
【问题描述】:

它在我的本地和测试服务器中运行良好。但是在生产服务器中部署它时。

我在使用此功能时收到内部服务器错误 500。

除此之外,所有功能都可以正常工作。

在视图中调用发送邮件的函数:

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");
              }
         });
}

控制器

[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();
}

如果有人可以帮助我。非常感谢。谢谢。

Error Result

【问题讨论】:

  • 您需要将日志记录添加到服务器端以缩小问题所在。
  • 刷新响应然后return View();是很奇怪的。
  • 第 375 行是哪一行?
  • 实际回报是这样的。 return Json(new { success = true, responseText = "Email Sent to :" + MailTo }, JsonRequestBehavior.AllowGet);
  • @NiñoAngeloReyes 您声称它返回了 json,但您的堆栈跟踪却说否则,您在服务器上遇到了未处理的异常。生产环境中邮件服务器是否可达?

标签: c# jquery ajax asp.net-mvc


【解决方案1】:

问题不在于您的 Javascript 函数,而在于您的控制器方法 SendMailToFutureHold

从堆栈错误我们可以读取null reference exception object not set to an instance of an object

假设DataTable dt = MRB.GetDataForAttachmentForFH(MRBNumber, MailTo, mrb_type);被正确初始化(非空)-

Response 可能未初始化。

确定的唯一方法是进行一些日志记录——据我所知,这是调试生产代码的唯一方法。

你可以试试log4net

【讨论】:

  • 谢谢亚历克斯。 log4Net 可以定义哪一行遇到错误。我终于修复了内部错误 500。
  • @NiñoAngeloReyes 很高兴它有帮助 - 登录总是一个好主意 - 可以让你免去长时间的头痛 - 请不要忘记投票 - 为了帮助他人
【解决方案2】:

基本上这个问题与你无关front-end code。它来自您在Ajax call 中消费的back-end method

我想说,在 .cs 方法中使用 Try 和 Catch 并将错误记录在 catch 中。因为 500 internal server error 是由于您的代码中的运行时错误而出现的。

创建你自己的Logger类,你可以在你的项目中实现Log4net并将日志写入你的服务器。通过这种方式,您可以得到答案是什么原因导致您的Ajax Response 中出现500 Internal Server 错误

[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
   }
}

【讨论】:

    猜你喜欢
    • 2019-11-29
    • 1970-01-01
    • 2011-03-01
    • 2020-06-03
    • 2020-03-12
    • 2018-05-08
    • 2013-09-04
    • 2015-02-07
    • 2018-01-22
    相关资源
    最近更新 更多