UseStatusCodePagesWithReExecute未处理的异常重复键:System.Object

时间:2017-01-20 02:16:27

标签: asp.net-core asp.net-core-mvc unhandled-exception asp.net-core-middleware

AsP.Net Core UseStatusCodePagesWithReExecute(),我正在重定向到/StatusCode/{0}。我以前使用UseStatusCodePagesWithRedirect()时没有任何明显的问题但是自从更改之后,之前重定向的请求会导致未处理的异常:

  

ArgumentException:已添加具有相同键的项。键:System.Object

这是VS15调试窗口的输出:

Microsoft.AspNetCore.Hosting.Internal.WebHost:Information: Request starting HTTP/1.1 GET http://localhost:5000/guides/some-guide/introdu  
Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker:Information: Executing action method WebSite.Controllers.GuideController.ReadSection (WebSite) with arguments (some-guide, introdu) - ModelState is Valid
Microsoft.EntityFrameworkCore.Storage.IRelationalCommandBuilderFactory:Information: Executed DbCommand (0ms) [Parameters=[@__permalink_0='?' (Size = 450)], CommandType='Text', CommandTimeout='30']
SELECT TOP(2) [s].[SectionId], [s].[Description], [s].[DisplayPosition], [s].[DocumentId], [s].[Permalink], [s].[Title], [d].[DocumentId], [d].[CategoryId], [d].[Description], [d].[DisplayPosition], [d].[Permalink], [d].[Subtitle], [d].[Title]
FROM [Sections] AS [s]
INNER JOIN [Documents] AS [d] ON [s].[DocumentId] = [d].[DocumentId]
WHERE [s].[Permalink] = @__permalink_0
ORDER BY [s].[SectionId]
Microsoft.AspNetCore.Mvc.StatusCodeResult:Information: Executing HttpStatusCodeResult, setting HTTP status code 404
Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker:Information: Executed action WebSite.Controllers.GuideController.ReadSection (WebSite) in 15.6192ms
Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker:Information: Executing action method WebSite.Controllers.ErrorController.StatusCode (WebSite) with arguments (404) - ModelState is Valid
WebSite.Controllers.ErrorController:Information: Unexpected Status Code: 404, OriginalPath: /guides/some-guide/introdu
'dotnet.exe' (CoreCLR: clrhost): Loaded 'g4tz3vmn.35h'. Cannot find or open the PDB file.
Microsoft.AspNetCore.Mvc.ViewFeatures.Internal.ViewResultExecutor:Information: Executing ViewResult, running view at path /Views/Error/Index.cshtml.
Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker:Information: Executed action WebSite.Controllers.ErrorController.StatusCode (WebSite) in 86.9564ms
'dotnet.exe' (CoreCLR: clrhost): Loaded 'C:\Users\Me\.nuget\packages\System.Diagnostics.StackTrace\4.3.0\lib\netstandard1.3\System.Diagnostics.StackTrace.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'dotnet.exe' (CoreCLR: clrhost): Loaded 'C:\Program Files\dotnet\shared\Microsoft.NETCore.App\1.1.0\System.IO.MemoryMappedFiles.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'dotnet.exe' (CoreCLR: clrhost): Loaded 'C:\Program Files\dotnet\shared\Microsoft.NETCore.App\1.1.0\System.IO.UnmanagedMemoryStream.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware:Error: An unhandled exception has occurred while executing the request

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 WebMarkupMin.AspNetCore1.WebMarkupMinMiddleware.<Invoke>d__5.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.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()
Microsoft.AspNetCore.Hosting.Internal.WebHost:Information: Request finished in 342.116ms 500 text/html; charset=utf-8

看起来它正在执行我的StatusCode控制器操作正常,记录错误的原始路径然后bam,我得到异常。导致此错误的原因是什么?

4 个答案:

答案 0 :(得分:1)

堆栈很难读取,因为它是发布版本中的异步代码,但它与EF没有任何关系。我不得不调试MVC的源代码来跟踪这个。

这似乎是中间件中的一个错误,它负责保存TempData。该路径的代码不同in the latest version of MVC

这是有趣的部分:至少在我看来,触发器似乎是BrowserLink。所以如果你的Startup.cs包含

app.UseBrowserLink();

在任何时候,尝试评论它,看看它是否有帮助。

答案 1 :(得分:0)

我不确定你为什么会遇到这个例外。

出于测试目的,我在VS 2015中创建了一个新的ASP.NET Core Web Application

我选择了Web Application模板来获取基本内容。

Configure()文件的Startup.cs方法内,在app.UseStaticFiles();上方,我添加了以下内容:

app.UseStatusCodePagesWithReExecute("/Home/Error2/{0}");

然后我在Error2内添加了HomeController ActionResult,如下所示:

public IActionResult Error2(string id)
{
    return View();
}

我在Views - Home文件夹中创建了Error2.cshtml视图。

我在return View();方法的Error2()上设置了一个断点,并在调试中启动了该应用程序。

我成功导航到About页面(http://localhost:63595/Home/About),然后故意添加错误来触发404,例如:http://localhost:63595/Home/Abouteeee

我的断点被击中,string id参数持有“404”。

我知道这可能不会回答你的问题,但可能会采取一些小步骤来让事情先发挥作用,然后逐渐添加东西可能(或不会)帮助你找出罪魁祸首。

答案 2 :(得分:0)

我遇到了同样的问题而且没有使用BrowserLink。但就像Rytmis说的那样,它似乎确实是由TempData引起的。如果您未在任何地方使用TempData,请将Startup.cs中的AddMvc调用更改为以下内容:

 services.AddMvc(options =>
 {
     options.Filters.Remove(new SaveTempDataAttribute());
 });

小心那个,这是一个丑陋的黑客。

就我而言,root问题结果是我的错误控制器继承自使用ControllerBase.HttpContext属性的基类。一旦我改变它以从IHttpContextAccessor获取httpContext,问题就消失了。

答案 3 :(得分:0)

使用错误处理时,我会抛出相同的错误:

app.UseStatusCodePagesWithReExecute("/Error/{0}")

对我来说,问题是我在错误处理程序操作中使用了HttpGetAttribute:

[HttpGet("/Error/{status}")]
public IActionResult Error(string status) { ..

当我将属性更改为改为使用RouteAttribute时(即-也支持POST,PUT等),它起作用了:

[Route("/Error/{status}")]
public IActionResult Error(string status) { ..

查看更多... GitHub AspNetCore Issue