解决错误 - "返回404"已经添加了具有相同键的项目。

时间:2017-02-11 11:06:09

标签: c# asp.net-core .net-core

我的自定义错误页面处理程序定义如下:

if (env.IsDevelopment())
{
    app.UseDeveloperExceptionPage();
    app.UseBrowserLink();
}
else
{
    app.UseExceptionHandler("/Error");
}

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

app.UseMvc(routes =>
{
    ...
}

然后在错误控制器中:

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

        return View(errorCode);
    }
}

在生产模式下,一切正常,但在开发时,有两种情况:

  1. 我正在点击404页面"就像那样"并且它显示我的自定义404页面(这是正确的)
  2. 我强迫404采取行动,例如。

    if(nothingFound)
    {
        return NotFound();
    }
    

    然后它进入ErrorController操作,甚至是视图(404.cshtml),但之后它会打印标题为的全部调试:

  3.   

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

    使用app.UseDeveloperExceptionPage()时是否正常,或者我是否有错误配置的内容?

    更新

    堆栈追踪:

    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()
    

3 个答案:

答案 0 :(得分:2)

错误是由于对UseBrowserLink()的调用,您可以从相关的menù管理此功能。

如果取消选中启用浏览器链接,则错误消失。

enter image description here

我不知道导致问题的原因,但这里是指向UseBrowserLink docs

的链接

答案 1 :(得分:2)

我遇到了类似的问题。后来我发现问题与使用中间件的顺序有关。我使用中间件来编写响应头像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}");
        }

希望这可以帮助任何有类似问题的人。

答案 2 :(得分:0)

我看到的是UseStatusCodePagesWithReExecute,它指向一个不存在的端点,同时我试图获取标有[HttpPost]的端点。