将当前URL与routedata一起传递给自定义错误控制器

时间:2013-11-01 13:25:30

标签: c# asp.net-mvc asp.net-mvc-4

我正在尝试将访问过的url传递给名为ErrorController的错误控制器,以便我可以记录当时正在访问的页面。

在我的Global.asax.cs中,我有一个方法Application_Error,如下所示:

protected void Application_Error(object sender, EventArgs e)
{
    var httpContext = ((MvcApplication)sender).Context;

    var currentRouteData = RouteTable.Routes.GetRouteData(new HttpContextWrapper(httpContext));
    var currentController = " ";
    var currentAction = " ";

    if (currentRouteData != null)
    {
        if (currentRouteData.Values["controller"] != null && !String.IsNullOrEmpty(currentRouteData.Values["controller"].ToString()))
        {
            currentController = currentRouteData.Values["controller"].ToString();
        }

        if (currentRouteData.Values["action"] != null && !String.IsNullOrEmpty(currentRouteData.Values["action"].ToString()))
        {
            currentAction = currentRouteData.Values["action"].ToString();
        }
    }

    var ex = Server.GetLastError();

    var controller = new ErrorController();
    var routeData = new RouteData();
    var action = "Index";

    if (ex is HttpException)
    {
        var httpEx = ex as HttpException;

        switch (httpEx.GetHttpCode())
        {
            case 404:
                action = "NotFound";
                // Pass along some data about accessed page here
                break;

            // others if any

            default:
                action = "Index";
                break;
        }
    }

    httpContext.ClearError();
    httpContext.Response.Clear();
    httpContext.Response.StatusCode = ex is HttpException ? ((HttpException)ex).GetHttpCode() : 500;
    httpContext.Response.TrySkipIisCustomErrors = true;
    routeData.Values["controller"] = "Error";
    routeData.Values["action"] = action;

    controller.ViewData.Model = new HandleErrorInfo(ex, currentController, currentAction);
    ((IController)controller).Execute(new RequestContext(new HttpContextWrapper(httpContext), routeData));
} 

我的ErrorController看起来像这样:

public class ErrorController : BaseController
{
    private readonly ILog _logger;
    public ErrorController()
    {
        _logger = LogManager.GetLogger("CustomHandleErrorAttribute.class");
    }

    //
    // GET: /Error/

    public ActionResult Index()
    {
        return View();
    }

    public ActionResult NotFound(string error)
    {
        _logger.Error(error);
        return View();
    }
}

我应该如何填充错误参数,以便将其记录到我的文件中?

2 个答案:

答案 0 :(得分:0)

我认为你有点过于复杂了。只需创建一个共享函数,在错误控制器类中记录异常和页面。这样你就可以忘记一起路由。您可以在请求中使用UrlReferrer属性来获取发生错误的页面。

Global.asax(vb.net):

Sub Application_Error()

    Dim ex As Exception = Server.GetLastError()
    Dim page As String = If(Not IsNothing(Request), Request.UrlReferrer.AbsoluteUri, Nothing)
    ErrorController.LogError(ex, page)

    Server.ClearError()

End Sub

答案 1 :(得分:0)

ErrorController.cs

中完成以下操作:
if (Request.Url != null)
{
    var path = Request.Url.AbsoluteUri;
    _logger.Error("404: " + path);
}