簡體   English   中英

設置404響應代碼時,為什么會出現System.Threading.ThreadAbortException?

[英]Why am I Getting System.Threading.ThreadAbortException when setting a 404 response code?

我的應用程序中包含以下代碼。

如果_showConfiguration為null,並且Response對象為NOT null,則狀態代碼設置為404,並且響應結束。

那就是以前的開發人員寫的方式。 現在,當我們創建一個包含ShowConfiguration對象的新頁面時,它總是拋出System.Threading.ThreadAbortException。

我想知道是什么原因造成的。 請指教。

謝謝

public ShowConfiguration ShowConfig
{
    get
    {
        if (_showConfiguration == null)
        {
            _showConfiguration = ShowConfigurationCache.GetShowByAliasPath(_aliasPath);


            if (_showConfiguration == null && Response != null)
            {
                Response.Clear();
                Response.StatusCode = 404;
                Response.End();
            }


        }

        return _showConfiguration;
    }
}

因為這是ASP.NET中Response的本質。 即使執行Response.Redirect也會導致ThreadAbortException,因為它已完成對當前請求的處理。 為了避免這種情況,將第二個參數傳遞為false可以避免這種情況。 請參閱: https : //support.microsoft.com/en-us/kb/312629

當然,這取決於您的意圖,但是默認情況下,這是ASP.NET中的行為。

這也可能使您對Response.End有一些了解:

http://blogs.msdn.com/b/tmarq/archive/2009/06/25/correct-use-of-system-web-httpresponse-redirect.aspx

總結是Response.End方法結束了當前頁面的執行,並將其執行移至應用程序事件管道中的Application_EndRequest事件。 Response.End之后的任何代碼行都不會執行,但會引發異常。

您應該在做HttpContext.Current.ApplicationInstance.CompleteRequest。 但同樣,您所看到的行為是正常且預期的

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM