错误。已添加具有相同键的项目

时间:2014-09-12 08:29:07

标签: c# asp.net asp.net-mvc asp.net-web-api

我在Web应用程序中获得了一些处理Http错误的代码:

protected void Application_EndRequest()
{
    int code = Context.Response.StatusCode;

    switch (code)
    {
        case 404:
            {
                ExecuteErrorRequest();
                break;
            }
        case 403:
            {
                ExecuteErrorRequest();
                break;
            }
    }
}

private void ExecuteErrorRequest()
{            
    Response.Clear();
    var rd = new RouteData();
    rd.Values["controller"] = "Error";
    rd.Values["action"] = "Index";
    rd.Values["errorCode"] = Response.StatusCode;
    rd.Values["errorMsg"] = Response.StatusDescription;
    IController c = new ErrorController();
    c.Execute(new RequestContext(new HttpContextWrapper(Context), rd));
}

和错误控制器:

public ActionResult Index(int errorCode, string errorMsg)
{
    ViewBag.Url = Request.Url.PathAndQuery;
    ViewBag.Code = errorCode;
    ViewBag.Msg = errorMsg;

    return View();
}

此代码:

((IController)c).Execute(new RequestContext(new HttpContextWrapper(Context), rd)); 

有时会引发异常:

  

类型' System.ArgumentException'的例外情况发生在   System.Web.Mvc.dll但未在用户代码中处理

     

附加信息:具有相同密钥的项目已经存在   加入。

如果在ErrorController的Index操作中删除参数

public ActionResult Index()

并像这样填写RouteData:

var rd = new RouteData();
rd.Values["controller"] = "Error";
rd.Values["action"] = "Index";

异常不会出现。我没有任何想法为什么出现这个例外...

*

UPD。 StackTrace:

  

     
    

System.ThrowHelper.ThrowArgumentException(ExceptionResource resource)     在System.Collections.Generic.Dictionary 2.Insert(TKey key, TValue value, Boolean add) at System.Collections.Generic.Dictionary 2.添加(TKey键,TValue值)
    在     System.Web.Mvc.JsonValueProviderFactory.EntryLimitedDictionary.Add(字符串     key,Object value)at     System.Web.Mvc.JsonValueProviderFactory.AddToBackingStore(EntryLimitedDictionary     backingStore,String prefix,Object value)at     System.Web.Mvc.JsonValueProviderFactory.AddToBackingStore(EntryLimitedDictionary     backingStore,String prefix,Object value)at     System.Web.Mvc.JsonValueProviderFactory.AddToBackingStore(EntryLimitedDictionary     backingStore,String prefix,Object value)at     System.Web.Mvc.JsonValueProviderFactory.AddToBackingStore(EntryLimitedDictionary     backingStore,String prefix,Object value)at     System.Web.Mvc.JsonValueProviderFactory.GetValueProvider(ControllerContext     controllerContext)at     System.Web.Mvc.ValueProviderFactoryCollection.GetValueProvider(ControllerContext     controllerContext)at     System.Web.Mvc.ControllerBase.get_ValueProvider()at     System.Web.Mvc.ControllerActionInvoker.GetParameterValue(ControllerContext     controllerContext,ParameterDescriptor parameterDescriptor)at     System.Web.Mvc.ControllerActionInvoker.GetParameterValues(ControllerContext     controllerContext,ActionDescriptor actionDescriptor)at     System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext     controllerContext,String actionName)at     System.Web.Mvc.Controller.ExecuteCore()at     System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext)
    在     System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(的RequestContext     requestContext)at     ConstractionInvestments.Web.WebApiApplication.ExecuteErrorRequest()in     C:\开发\项目\ Mosaip \ ConstractionInvestments \ ConstractionInvestments.Web \的Global.asax.cs:行     52点     ConstractionInvestments.Web.WebApiApplication.Application_EndRequest()     在     C:\开发\项目\ Mosaip \ ConstractionInvestments \ ConstractionInvestments.Web \的Global.asax.cs:行     36

  
     

*

0 个答案:

没有答案