在全局中创建Application_Error处理程序.asax文件得到解析器错误

本文关键字:文件 错误 asax 程序 创建 全局 Application 处理 Error | 更新日期: 2023-09-27 18:14:13

我试图将Application_Error方法添加到全局。asax文件,但我得到一个解析器错误:

'/'应用程序中的服务器错误。解析器错误描述:错误在解析为此提供服务所需的资源期间发生请求。请查看以下具体的解析错误细节和修改你的源文件

解析器错误消息:应用程序文件中的内容不是有效。

我不明白为什么这个方法不起作用。任何帮助都很感激,谢谢!

<%@Application Language='C#' Inherits="Sitecore.Web.Application" %>
using System.Configuration;
protection void Application_Error(object sender, EventArgs e)
{
  // Code that runs when an unhandled error occurs
  // Get the exception object.
  Exception exc = Server.GetLastError();
  // Handle HTTP errors
  if (exc.GetType() == typeof(HttpException))
  {
    // The Complete Error Handling Example generates
    // some errors using URLs with "NoCatch" in them;
    // ignore these here to simulate what would happen
    // if a global.asax handler were not implemented.
    if (exc.Message.Contains("NoCatch") || exc.Message.Contains("maxUrlLength"))
    return;
   //Redirect HTTP errors to HttpError page
   Server.Transfer("HttpErrorPage.aspx");
 }
 // For other kinds of errors give the user some information
 // but stay on the default page
 Response.Write("<h2>Global Page Error</h2>'n");
 Response.Write(
     "<p>" + exc.Message + "</p>'n");
 Response.Write("Return to the <a href='Default.aspx'>" +
  "Default Page</a>'n");
  // Log the exception and notify system operators
  ExceptionUtility.LogException(exc, "DefaultPage");
  ExceptionUtility.NotifySystemOps(exc);
  // Clear the error from the server
  Server.ClearError();
}

在全局中创建Application_Error处理程序.asax文件得到解析器错误

我认为你应该在。cs文件中添加代码。

而且保护是不被接受的。你的代码应该是这样的:

void Application_Error(object sender, EventArgs e)
{            
  //DoSomething();
}