ASP.NET MVC中的异步错误控制器

Mon*_*ode 1 c# error-handling asp.net-mvc asynchronous asp.net-mvc-4

任何人都可以帮助我在ASP MVC中使用异步错误控制器.我把它全部工作,没有它是异步的,但这似乎不对.我开始的代码如下:

void Application_Error(object sender, EventArgs e)
{
        Exception exception = Server.GetLastError();

        // A good location for any error logging, otherwise, do it inside of the error controller.

        // Clear the error, otherwise, we will always get the default error page.
        Server.ClearError();
        Response.Clear();
        Response.TrySkipIisCustomErrors = true;

        var code = (exception is HttpException) ? (exception as HttpException).GetHttpCode() : 500;

        var routeData = new RouteData();
        routeData.Values.Add("controller", "Error");
        routeData.Values.Add("action", "index");
        IController errorController = ClassFactory.Resolve<ErrorController>();
        errorController.Execute(new RequestContext(new HttpContextWrapper(Context), routeData));
}
Run Code Online (Sandbox Code Playgroud)

我的错误控制器的方法签名如下:

    [AllowAnonymous]
    public ActionResult Index(int code = 500)
    {
Run Code Online (Sandbox Code Playgroud)

如果我使错误控制器方法异步,Application_Error中的Execute方法抛出异常"异步操作方法'索引'返回一个无法同步执行的任务."

我最有希望的尝试之一是转移请求.这在applicaiton_error方法中使用了以下代码:

var code = (exception is HttpException) ? (exception as HttpException).GetHttpCode() : 500;
var headers = new NameValueCollection
{
    {"ErrorUrl", Context.Request.RawUrl},
    {"ErrorCode", code.ToString()}
};
Context.Server.TransferRequest("~/error", false, null, headers, true);
Run Code Online (Sandbox Code Playgroud)

这很好,异步,我可以在请求标头中传递错误数据.不幸的是,Server.TransferRequest调用丢失了有关当前登录用户的详细信息,并且用户将在注销时出现在错误页面上.用户登录代码当前与VS2013模板的用户登录代码相同(不是旧表单auth).preserveUser参数似乎不起作用.

有没有人有这样的异步控制器工作?

要么

有谁知道如何解决在TransferRequest上丢失的ASP身份?

相关问题包括在使用前缀异步时不会触发Application_Error处理程序,该异常具有比不使用异步(一次使用两个线程)更糟糕的解决方案.另一个回答类似于我的问题,但没有TransferRequest.

Mon*_*ode 5

Context.Server.TransferRequest是解决问题的正确方法.此函数通过整个IIS管道发送以下请求.我忘掉ASP身份的原因是它存储在Session中.会话使用cookie链接到客户端.Cookie在标题中.

而不是代码:

var code = (exception is HttpException) ? (exception as HttpException).GetHttpCode() : 500;
var headers = new NameValueCollection
{
    {"ErrorUrl", Context.Request.RawUrl},
    {"ErrorCode", code.ToString()}
};
Context.Server.TransferRequest("~/error", false, null, headers, true);
Run Code Online (Sandbox Code Playgroud)

我有它的工作:

var code = (exception is HttpException) ? (exception as HttpException).GetHttpCode() : 500;
var headers = Request.Headers;
headers.Add(new NameValueCollection
{
    {"ErrorUrl", Context.Request.RawUrl},
    {"ErrorCode", code.ToString()}
});
Context.Server.TransferRequest("~/error", false, null, headers, true);
Run Code Online (Sandbox Code Playgroud)

主要区别在于我传入当前请求的标头,其中包含链接到会话的cookie.这可以阻止ASP.NET标识丢失.