2017-02-11 52 views
2

我已經定義是這樣我的自定義錯誤頁處理程序「具有相同鍵的項回到404的時候已經被添加」排除錯誤

public class ErrorController : Controller 
{ 
    public IActionResult Index(int errorCode) 
    { 
     switch (errorCode) 
     { 
      case 404: 
       return View($"~/Views/Error/{errorCode}.cshtml"); 
     } 

     return View(errorCode); 
    } 
} 

當在生產模式下一切正常,但在開發時,有兩種情況:

  1. 我打404頁「就像那樣」,我t的顯示我的自定義404頁面(這是正確的)
  2. 我強迫404從行動例如。

    if(nothingFound) 
    { 
        return NotFound(); 
    } 
    

    ,然後它進入ErrorController行動,甚至視圖(404.cshtml),但它打印後整個調試標題:

的ArgumentException:具有相同鍵的項已被添加。 Key:System.Object

使用app.UseDeveloperExceptionPage()時是否正常?還是我有一些未配置的東西?

UPDATE

堆棧跟蹤:

System.ArgumentException: An item with the same key has already been added. Key: System.Object 
    at System.ThrowHelper.ThrowAddingDuplicateWithKeyArgumentException(Object key) 
    at System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add) 
    at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted) 
    at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.<InvokeNextResourceFilter>d__22.MoveNext() 
--- End of stack trace from previous location where exception was thrown --- 
    at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() 
    at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Rethrow(ResourceExecutedContext context) 
    at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted) 
    at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.<InvokeAsync>d__20.MoveNext() 
--- End of stack trace from previous location where exception was thrown --- 
    at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() 
    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) 
    at Microsoft.AspNetCore.Builder.RouterMiddleware.<Invoke>d__4.MoveNext() 
--- End of stack trace from previous location where exception was thrown --- 
    at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() 
    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) 
    at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware`1.<Invoke>d__18.MoveNext() 
--- End of stack trace from previous location where exception was thrown --- 
    at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() 
    at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware`1.<Invoke>d__18.MoveNext() 
--- End of stack trace from previous location where exception was thrown --- 
    at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() 
    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) 
    at Microsoft.AspNetCore.Builder.StatusCodePagesExtensions.<>c__DisplayClass6_0.<<UseStatusCodePagesWithReExecute>b__0>d.MoveNext() 
--- End of stack trace from previous location where exception was thrown --- 
    at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() 
    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) 
    at Microsoft.AspNetCore.Diagnostics.StatusCodePagesMiddleware.<Invoke>d__3.MoveNext() 
--- End of stack trace from previous location where exception was thrown --- 
    at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() 
    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) 
    at Microsoft.AspNetCore.Localization.RequestLocalizationMiddleware.<Invoke>d__4.MoveNext() 
--- End of stack trace from previous location where exception was thrown --- 
    at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() 
    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) 
    at Microsoft.VisualStudio.Web.BrowserLink.Runtime.BrowserLinkMiddleware.<ExecuteWithFilter>d__7.MoveNext() 
--- End of stack trace from previous location where exception was thrown --- 
    at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() 
    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) 
    at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.<Invoke>d__7.MoveNext() 
+0

你是說異常消息顯示在開發服務器而不是達到ErrorController? – Kira

+0

請發佈該異常的堆棧跟蹤。 –

+0

@Kira是的,但只有當我強迫錯誤使用某些功能,例如。未找到()。當訪問非現有路線時,它既適用於生產也適用於開發。我完全沒有要求在開發模式下到達錯誤控制器,但我希望除了這個例外以外。 –

回答

2

的錯誤是由於調用UseBrowserLink(),您可以管理相關的菜單此功能。

如果取消選中啓用瀏覽器鏈接錯誤消失。

enter image description here

我不知道是什麼引起的問題,但這裏是鏈接到UseBrowserLink docs

+0

也許你可以擴展你的答案來解釋導致這個錯誤的瀏覽器鏈接在這裏做什麼? – poke

0

我有類似的問題。後來我發現問題與我使用中間件的順序有關。我正在使用中間件編寫響應頭文件,如X-Content-Type-Options和其他頭文件。我剛剛將這個移動到了app.UseStatusCodePagesWithReExecute之上,它開始正常工作。所以我目前的代碼是這樣的:

app.Use(async (context, next) => 
     { 
      context.Response.Headers.Add("X-Content-Type-Options", "nosniff"); 
      context.Response.Headers.Add("X-Xss-Protection", "1"); 
      context.Response.Headers.Add("X-Frame-Options", "SAMEORIGIN"); 
      await next(); 
     }); 

     if (env.IsDevelopment()) 
     { 
      app.UseDeveloperExceptionPage(); 
      app.UseDatabaseErrorPage(); 
      app.UseBrowserLink(); 
     } 
     else 
     { 
      app.UseStatusCodePagesWithReExecute("/Home/Error/{0}"); 
     } 

希望這可以幫助像我這樣的類似問題的任何人。