抛出一个exeption时,MVC返回无效的json

时间:2017-09-12 09:27:49

标签: c# json angular model-view-controller

我有简单的功能

public ActionResult GetUserList(int? departmentid)
        {
                return Content(list.GetJson(departmentid), "application/Json");    
        }

我用Angular称呼它

$http.post('/StockAssignment/GetUserList', { departmentid: departmentId}).then(
    function (data) {  /* all ok, do stuff */ }, 
    function(data) { /* show the error, Witch should be in data.data.msg }
);

这很好用,直到list.GetJson函数中的某些内容抛出错误。我想使用angular显示该错误,但结果是无效的JSON。以下是返回数据的示例:

{ "msg" : "Unable to process the type 'Anonymous type', because it has no known mapping to the value layer.   at System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.NewArrayInitTranslator.TypedTranslate(ExpressionConverter parent, NewArrayExpression linq)
   at System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.TranslateExpression(Expression linq)
   at System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.ConstantTranslator.TypedTranslate(ExpressionConverter parent, ConstantExpression linq)
   at System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.TranslateExpression(Expression linq)
   at System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.MethodCallTranslator.GroupJoinTranslator.Translate(ExpressionConverter parent, MethodCallExpression call)
   at System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.MethodCallTranslator.TypedTranslate(ExpressionConverter parent, MethodCallExpression linq)
   at System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.TranslateExpression(Expression linq)
   at System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.MethodCallTranslator.OneLambdaTranslator.Translate(ExpressionConverter parent, MethodCallExpression call, DbExpression& source, DbExpressionBinding& sourceBinding, DbExpression& lambda)
   at System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.MethodCallTranslator.SelectManyTranslator.Translate(ExpressionConverter parent, MethodCallExpression call)
   at System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.MethodCallTranslator.TypedTranslate(ExpressionConverter parent, MethodCallExpression linq)
   at System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.TranslateExpression(Expression linq)
   at System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.Convert()
   at System.Data.Entity.Core.Objects.ELinq.ELinqQueryState.GetExecutionPlan(Nullable`1 forMergeOption)
   at System.Data.Entity.Core.Objects.ObjectQuery`1.<>c__DisplayClass7.<GetResults>b__6()
   at System.Data.Entity.Core.Objects.ObjectContext.ExecuteInTransaction[T](Func`1 func, IDbExecutionStrategy executionStrategy, Boolean startLocalTransaction, Boolean releaseConnectionOnSuccess)
   at System.Data.Entity.Core.Objects.ObjectQuery`1.<>c__DisplayClass7.<GetResults>b__5()
   at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute[TResult](Func`1 operation)
   at System.Data.Entity.Core.Objects.ObjectQuery`1.GetResults(Nullable`1 forMergeOption)
   at System.Data.Entity.Core.Objects.ObjectQuery`1.<System.Collections.Generic.IEnumerable<T>.GetEnumerator>b__0()
   at System.Data.Entity.Internal.LazyEnumerator`1.MoveNext()
   at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeList(JsonWriter writer, IEnumerable values, JsonArrayContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter writer, Object value, JsonObjectContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter writer, Object value, JsonObjectContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeList(JsonWriter writer, IEnumerable values, JsonArrayContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.Serialize(JsonWriter jsonWriter, Object value, Type objectType)
   at Newtonsoft.Json.JsonSerializer.SerializeInternal(JsonWriter jsonWriter, Object value, Type objectType)
   at Norconsult.Extensions.JsonHelpers.GetJson(Object o) in C:\Users\hp\Documents\Visual Studio 2017\Projects\Norconsult\Norconsult\Extensions\JsonHelpers.cs:line 57"}

Angular无法读取此内容并引发错误:

Error: [$http:baddata] http://errors.angularjs.org/1.6.5/$http/baddata?p0=%7B%20%22msg%22%20%3A%20%22Unable%20to%20process%20the%20type%20'Anonymous%20type'%2C%20because%20it%20has%20no%20known%20mapping%20to%20the%20value%20layer.%20%20%20at%20System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.NewArrayInitTranslator.TypedTranslate(ExpressionConverter%20parent%2C%20NewArrayExpression%20linq)%0D%0A%20%20%20at%20System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.TranslateExpression(Expression%20linq)%0D%0A%20%20%20at%20System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.ConstantTranslator.TypedTranslate(ExpressionConverter%20parent%2C%20ConstantExpression%20linq)%0D%0A%20%20%20at%20System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.TranslateExpression(Expression%20linq)%0D%0A%20%20%20at%20System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.MethodCallTranslator.GroupJoinTranslator.Translate(ExpressionConverter%20parent%2C%20MethodCallExpression%20call)%0D%0A%20%20%20at%20System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.MethodCallTranslator.TypedTranslate(ExpressionConverter%20parent%2C%20MethodCallExpression%20linq)%0D%0A%20%20%20at%20System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.TranslateExpression(Expression%20linq)%0D%0A%20%20%20at%20System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.MethodCallTranslator.OneLambdaTranslator.Translate(ExpressionConverter%20parent%2C%20MethodCallExpression%20call%2C%20DbExpression%26%20source%2C%20DbExpressionBinding%26%20sourceBinding%2C%20DbExpression%26%20lambda)%0D%0A%20%20%20at%20System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.MethodCallTranslator.SelectManyTranslator.Translate(ExpressionConverter%20parent%2C%20MethodCallExpression%20call)%0D%0A%20%20%20at%20System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.MethodCallTranslator.TypedTranslate(ExpressionConverter%20parent%2C%20MethodCallExpression%20linq)%0D%0A%20%20%20at%20System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.TranslateExpression(Expression%20linq)%0D%0A%20%20%20at%20System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.Convert()%0D%0A%20%20%20at%20System.Data.Entity.Core.Objects.ELinq.ELinqQueryState.GetExecutionPlan(Nullable%601%20forMergeOption)%0D%0A%20%20%20at%20System.Data.Entity.Core.Objects.ObjectQuery%601.%3C%3Ec__DisplayClass7.%3CGetResults%3Eb__6()%0D%0A%20%20%20at%20System.Data.Entity.Core.Objects.ObjectContext.ExecuteInTransaction%5BT%5D(Func%601%20func%2C%20IDbExecutionStrategy%20executionStrategy%2C%20Boolean%20startLocalTransaction%2C%20Boolean%20releaseConnectionOnSuccess)%0D%0A%20%20%20at%20System.Data.Entity.Core.Objects.ObjectQuery%601.%3C%3Ec__DisplayClass7.%3CGetResults%3Eb__5()%0D%0A%20%20%20at%20System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute%5BTResult%5D(Func%601%20operation)%0D%0A%20%20%20at%20System.Data.Entity.Core.Objects.ObjectQuery%601.GetResults(Nullable%601%20forMergeOption)%0D%0A%20%20%20at%20System.Data.Entity.Core.Objects.ObjectQuery%601.%3CSystem.Collections.Generic.IEnumerable%3CT%3E.GetEnumerator%3Eb__0()%0D%0A%20%20%20at%20System.Data.Entity.Internal.LazyEnumerator%601.MoveNext()%0D%0A%20%20%20at%20Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeList(JsonWriter%20writer%2C%20IEnumerable%20values%2C%20JsonArrayContract%20contract%2C%20JsonProperty%20member%2C%20JsonContainerContract%20collectionContract%2C%20JsonProperty%20containerProperty)%0D%0A%20%20%20at%20Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter%20writer%2C%20Object%20value%2C%20JsonObjectContract%20contract%2C%20JsonProperty%20member%2C%20JsonContainerContract%20collectionContract%2C%20JsonProperty%20containerProperty)%0D%0A%20%20%20at%20Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter%20writer%2C%20Object%20value%2C%20JsonObjectContract%20contract%2C%20JsonProperty%20member%2C%20JsonContainerContract%20collectionContract%2C%20JsonProperty%20containerProperty)%0D%0A%20%20%20at%20Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeList(JsonWriter%20writer%2C%20IEnumerable%20values%2C%20JsonArrayContract%20contract%2C%20JsonProperty%20member%2C%20JsonContainerContract%20collectionContract%2C%20JsonProperty%20containerProperty)%0D%0A%20%20%20at%20Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.Serialize(JsonWriter%20jsonWriter%2C%20Object%20value%2C%20Type%20objectType)%0D%0A%20%20%20at%20Newtonsoft.Json.JsonSerializer.SerializeInternal(JsonWriter%20jsonWriter%2C%20Object%20value%2C%20Type%20objectType)%0D%0A%20%20%20at%20Norconsult.Extensions.JsonHelpers.GetJson(Object%20o)%20in%20C%3A%5CUsers%5Chp%5CDocuments%5CVisual%20Studio%202017%5CProjects%5CNorconsult%5CNorconsult%5CExtensions%5CJsonHelpers.cs%3Aline%2057%22%7D&p1=%7B%7D

我能做些什么来阅读这个例子?

1 个答案:

答案 0 :(得分:-1)

它指的是链接:

link

它告诉你这是糟糕的数据,还有更多。

msg%22%20%3A%20%22Unable%20to%20process%20the%20type%20'Anonymous%20type'%2C%20because%20it%20has%20no%20known%20mapping%20to%20the%20value%20layer.%20%20%20at%20System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.NewArrayInitTranslator.TypedTranslate

msg unable to process the type anonymous type because it has no known mapping to the value layer.System.Data.entity.Core.Objects.ELinq.ExpressionConverter.NewArrayInitTranslator.TypedTranslate(so on)

顺便说一句,这是网址中的Accii。