1

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

4 回答 4

1

堆栈很难阅读,因为它是发布版本中的异步代码,但它与 EF 没有任何关系。我必须使用 MVC 的源代码进行调试才能找到它。

这似乎是负责保存 TempData 的中间件中的一个错误。该路径的代码在最新版本的 MVC中有所不同。

不过,有趣的是:至少对我来说,触发器似乎是 BrowserLink。因此,如果您的 Startup.cs 包含

app.UseBrowserLink();

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

于 2017-02-28T17:49:11.887 回答
1

使用错误处理时,我遇到了同样的错误:

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 问题

于 2019-09-24T11:12:33.450 回答
1

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

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

我选择了Web Application模板来获取已经到位的基本内容。

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

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

然后我在里面添加了Error2ActionResultHomeController像这样:

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

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

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

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

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

我知道这可能无法回答您的问题,但可能先采取一些小步骤让事情开始运作,然后逐渐添加东西可能(或不)帮助您查明罪魁祸首。

于 2017-01-20T17:55:39.573 回答
0

我遇到了同样的问题并且没有使用 BrowserLink。但就像 Rytmis 所说,它似乎确实是由 TempData 引起的。如果您没有在任何地方使用 TempData,将AddMvcStartup.cs 中的调用更改为以下内容应该可以解决它:

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

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

就我而言,根本问题是我的错误控制器从使用该ControllerBase.HttpContext属性继承的基类。一旦我将其更改为从 IHttpContextAccessor 获取 httpContext,问题就消失了。

于 2017-03-31T18:15:39.250 回答