0
我已經簡單的功能MVC返回時exeption拋出
public ActionResult GetUserList(int? departmentid)
{
return Content(list.GetJson(departmentid), "application/Json");
}
我把這個用角
$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 }
);
這只是正常無效JSON,直到一些在list.GetJson函數拋出錯誤。我想使用角度來顯示該錯誤,但結果不是有效的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"}
角度是無法閱讀這一點,並拋出一個錯誤:
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
有什麼事是能夠讀取exeption?
我完全意識到這是不好的數據,我的問題是爲什麼exeption格式不正確(並且格式不正確,所以我可以用data.data.msg讀取msg) – devzero