HTML代码基于Razor代码。我正在尝试测试404错误。 HTML代码正确,但是,当路由不存在时,它将显示原始HTML代码。
我有一个专门用于处理所有404错误的错误控制器。
这是我的路线配置:
public static class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "404-NotFound",
url: "NotFound",
defaults: new { controller = "ErrorCode", action = "Index" }
);
routes.MapRoute(
name: "Error",
url: "{*url}",
defaults: new { controller = "ErrorCode", action = "Index" }
);
}
}
我测试了未找到路线。 HTML正确呈现(在/ NotFound上)。我不知道为什么在未定义路由时会显示原始HTML文本。
更多信息: 这是global.asax文件。我用这种方法实现了错误处理。我相信问题就在这里。
protected void Application_Error(object sender, EventArgs args)
{
HttpContext context = ((HttpApplication)sender).Context;
Exception ex = Server.GetLastError();
//Clears all content from the buffer stream
context.ClearError();
// Request info
string employerId = Session["EmployerId"]?.ToString() ?? string.Empty;
//ErrorViewModel errorViewModel = WcbHttpUtility.ParseError(new HttpRequestWrapper(context.Request), employerId, ex);
string errorMessage = $"Employer ID: {employerId} has error {ex.Message}";
ErrorViewModel errorViewModel = new ErrorViewModel { ErrorMessage = errorMessage };
WcbReportApp.WebInstance().ReportTelemetryLog.TrackException(ex, nameof(Application_Error));
// Clear the error from the server
Server.ClearError();
//Response.Redirect($"~/ErrorCode/Index/");
var routeData = new RouteData();
routeData.Values.Add("Controller", "ErrorCode");
routeData.Values.Add("Action", "Index");
routeData.Values.Add("Model", errorViewModel);
var errorCodeController = new ErrorCodeController();
IController controller = errorCodeController;
errorCodeController.ControllerContext = new ControllerContext { RouteData = routeData };
controller.Execute(new RequestContext(new HttpContextWrapper(context), routeData));
}
请帮助!
谢谢!