在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