0

在 Mono MVC5 控制器中,下面的代码用于返回自定义错误页面:

    public ActionResult Details(string id, string product)
    {
       ....
        if (productNotFound) {
            // http://www.eidias.com/blog/2014/7/2/mvc-custom-error-pages
            Response.Clear();
            Response.TrySkipIisCustomErrors = true;
            Response.Write(product + " product not exists");
            Response.StatusCode = (int)HttpStatusCode.NotFound;
            Response.End();
            return null;
        }

一段时间后,这会导致可以在 Application_Error 中捕获的 ThreadAbortException。看起来线程继续运行,直到达到超时。

如何解决此问题,以免发生异常?

SO中的答案需要修改web.config并且在不同的地方有很多代码。

我正在寻找一种简单的方法来返回自定义错误页面而无需在 MVC5 中修改 web.config。

如何在控制器中终止 thred 或有其他方法来避免异常。从 Applicaion_Error 记录异常,如果日志文件不包含此类异常,那就太糟糕了。

异常堆栈跟踪:

System.Threading.ThreadAbortException: 
  at (wrapper managed-to-native) System.Threading.Thread:Abort_internal (System.Threading.InternalThread,object)
  at System.Threading.Thread.Abort (System.Object stateInfo) [0x00000] in /usr/local/src/mono-4.4.1/mcs/class/corlib/System.Threading/Thread.cs:477 
  at System.Web.HttpResponse.End () [0x0001c] in /usr/local/src/mono-4.4.1/mcs/class/System.Web/System.Web/HttpResponse.cs:637 
  at System.Web.HttpResponseWrapper.End () [0x00000] in /usr/local/src/mono-4.4.1/mcs/class/System.Web/System.Web/HttpResponseWrapper.cs:275 
  at MyApp.Store.Controllers.StoreController.Details (System.String id, System.String product) <0x414f9210 + 0x0026b> in <filename unknown>:0 
  at (wrapper dynamic-method) System.Object:lambda_method (System.Runtime.CompilerServices.Closure,System.Web.Mvc.ControllerBase,object[])
  at System.Web.Mvc.ActionMethodDispatcher.Execute (System.Web.Mvc.ControllerBase controller, System.Object[] parameters) <0x40dee7d0 + 0x0002d> in <filename unknown>:0 
  at System.Web.Mvc.ReflectedActionDescriptor.Execute (System.Web.Mvc.ControllerContext controllerContext, IDictionary`2 parameters) <0x40dec2c0 + 0x000ff> in <filename unknown>:0 
  at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod (System.Web.Mvc.ControllerContext controllerContext, System.Web.Mvc.ActionDescriptor actionDescriptor, IDictionary`2 parameters) <0x40dec260 + 0x00033> in <filename unknown>:0 
  at System.Web.Mvc.Async.AsyncControllerActionInvoker+ActionInvocation.InvokeSynchronousActionMethod () <0x40dec230 + 0x00025> in <filename unknown>:0 
  at System.Web.Mvc.Async.AsyncControllerActionInvoker.<BeginInvokeSynchronousActionMethod>b__39 (IAsyncResult asyncResult, ActionInvocation innerInvokeState) <0x40dec080 + 0x00013> in <filename unknown>:0 
  at (wrapper delegate-invoke) System.Web.Mvc.Async.EndInvokeDelegate`2[System.Web.Mvc.Async.AsyncControllerActionInvoker+ActionInvocation,System.Web.Mvc.ActionResult]:invoke_TResult_IAsyncResult_TState (System.IAsyncResult,System.Web.Mvc.Async.AsyncControllerActionInvoker/ActionInvocation)
  at System.Web.Mvc.Async.AsyncResultWrapper+WrappedAsyncResult`2[TResult,TState].CallEndDelegate (IAsyncResult asyncResult) <0x40dec000 + 0x0006a> in <filename unknown>:0 
  at System.Web.Mvc.Async.AsyncResultWrapper+WrappedAsyncResultBase`1[TResult].End () <0x40debd80 + 0x00053> in <filename unknown>:0 
  at System.Web.Mvc.Async.AsyncResultWrapper.End[TResult] (IAsyncResult asyncResult, System.Object tag) <0x40debc30 + 0x0003b> in <filename unknown>:0 
  at System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod (IAsyncResult asyncResult) <0x418f4fc0 + 0x0002b> in <filename unknown>:0 
  at System.Web.Mvc.Async.AsyncControllerActionInvoker+AsyncInvocationWithFilters.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3d () <0x41984f10 + 0x0007a> in <filename unknown>:0 
  at System.Web.Mvc.Async.AsyncControllerActionInvoker+AsyncInvocationWithFilters+<>c__DisplayClass46.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3f () <0x40ddbd70 + 0x00038> in <filename unknown>:0 
4

0 回答 0