【问题标题】:Web.config File ErrorWeb.config 文件错误
【发布时间】:2011-04-19 14:04:41
【问题描述】:

我正在通过 godaddy.com 托管一个网站,这是链接:

http://floridaroadrunners.com/

这是我的 web.config 文件:

 <?xml version="1.0"?>

<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=169433
  -->

<configuration>
  <connectionStrings>
    <add name="ApplicationServices"
         connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnetdb.mdf;User Instance=true"
         providerName="System.Data.SqlClient" />
  </connectionStrings>

  <system.web>
    <compilation debug="true" targetFramework="4.0" />

    <authentication mode="Forms">
      <forms loginUrl="~/Account/Login.aspx" timeout="2880" />
    </authentication>

<customErrors mode="Off"/>

    <membership>
      <providers>
        <clear/>
        <add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider" connectionStringName="ApplicationServices"
             enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false"
             maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10"
             applicationName="/" />
      </providers>
    </membership>

    <profile>
      <providers>
        <clear/>
        <add name="AspNetSqlProfileProvider" type="System.Web.Profile.SqlProfileProvider" connectionStringName="ApplicationServices" applicationName="/"/>
      </providers>
    </profile>

    <roleManager enabled="false">
      <providers>
        <clear/>
        <add name="AspNetSqlRoleProvider" type="System.Web.Security.SqlRoleProvider" connectionStringName="ApplicationServices" applicationName="/" />
        <add name="AspNetWindowsTokenRoleProvider" type="System.Web.Security.WindowsTokenRoleProvider" applicationName="/" />
      </providers>
    </roleManager>

  </system.web>

  <system.webServer>
     <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>
</configuration>

我收到运行时错误:

运行时错误

描述:应用程序错误 发生在服务器上。目前的 为此的自定义错误设置 应用程序防止的细节 被查看的应用程序错误 远程(出于安全原因)。它 但是,可以通过浏览器查看 在本地服务器机器上运行。

我还设置了 customErrors mode = "off"。这里有什么问题?我正在使用带有 4.0 框架的 Visual Studio 2010。谢谢!

【问题讨论】:

  • 我编辑了我的问题。请看!
  • 您确定在服务器上提交了 customErrors off 吗?如果模式真的关闭,您应该会看到错误。
  • 当我遇到这种情况时,总是由于web.config格式本身的错误导致无法解析配置。您发布的内容看起来不错,所以我不知道...

标签: c# asp.net web-hosting web-deployment


【解决方案1】:

如果您的主机启用了customErrors,您可能会考虑自己捕获并记录异常,以便查看发生了什么。

有几个选项。先试试Elmah

其次,您可以使用您的日志库(我喜欢 NLog,但任何都可以),并在 Global.asax.cs 中捕获 Application_Error 事件。

protected void Application_Error(object sender, EventArgs e)
        {
            //first, find the exception.  any exceptions caught here will be wrapped
            //by an httpunhandledexception, which doesn't realy help us, so we'll
            //try to get the inner exception
            Exception exception = Server.GetLastError();
            if (exception.GetType() == typeof(HttpUnhandledException) && exception.InnerException != null)
            {
                exception = exception.InnerException;
            }

            //get a logger from the container
            ILogger logger = ObjectFactory.GetInstance<ILogger>();
            //log it
            logger.FatalException("Global Exception", exception);
        }

这是一个很好的功能,无论如何,即使您能够关闭 customErrors。

【讨论】:

  • 使用 Elmah 的好建议!只需 nuget 安装 elmah。
【解决方案2】:

服务器的machine.configapplicationHost.config 可能会覆盖您的web.config 设置。不幸的是,如果是这种情况,您真的无能为力,只能联系 GoDaddy 的支持热线。

【讨论】:

  • 确实如此,但对于共享主机方案而言,锁定该特定配置设置似乎很奇怪。
  • 这听起来有点苛刻,从他们那里。我不将它们用于托管,因此我无法确认或否认。
  • 我目前不使用它们,也从未使用过它们;我只是在没有真正推测他们的政策的情况下得出最有意义的结论。
【解决方案3】:

customErrors mode value Off 我认为是区分大小写的。请检查您的第一个字符是否大写。

【讨论】:

  • 我也看到了,但看起来 OP 在发布的 xml 中的大小写是正确的(问题后面发布的不是)
【解决方案4】:

您可以在 Global.asax 中捕获错误并发送带有异常的电子邮件。

在 Global.asax.cs 中:

 void Application_Error(object sender, EventArgs e)
        {
            // Code that runs when an unhandled error occurs
            Exception ex = Server.GetLastError();
            ExceptionHandler.SendExceptionEmail(ex, "Unhandled", this.User.Identity.Name, this.Request.RawUrl);
            Response.Redirect("~/ErrorPage.aspx"); // So the user does not see the ASP.net Error Message
        }

我的 ExceptionHandler 类中的方法:

class ExceptionHandler
    {
        public static void SendExceptionEmail(Exception ex, string ErrorLocation, string UserName, string url)
        {
            SmtpClient mailclient = new SmtpClient();
            try
            {
                string errorMessage = string.Format("User: {0}\r\nURL: {1}\r\n=====================\r\n{2}", UserName, url, AddExceptionText(ex));
                mailclient.Send(ConfigurationManager.AppSettings["ErrorFromEmailAddress"],
                                ConfigurationManager.AppSettings["ErrorEmailAddress"],
                                ConfigurationManager.AppSettings["ErrorEmailSubject"] + " = " + ErrorLocation,
                                errorMessage);
            }
            catch { }
            finally { mailclient.Dispose(); }
        }

        private static string AddExceptionText(Exception ex)
        {
            string innermessage = string.Empty;
            if (ex.InnerException != null)
            {
                innermessage = string.Format("=======InnerException====== \r\n{0}", ExceptionHandler.AddExceptionText(ex.InnerException));
            }
            string message = string.Format("Message: {0}\r\nSource: {1}\r\nStack:\r\n{2}\r\n\r\n{3}", ex.Message, ex.Source, ex.StackTrace, innermessage);
            return message;
        }
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-07
    • 1970-01-01
    • 2012-03-25
    相关资源
    最近更新 更多