简体   繁体   English

无法将“AttachmentDetailsDto”类型的对象转换为“System.String”类型

[英]Unable to cast object of type 'AttachmentDetailsDto' to type 'System.String'

Have method有方法

public async Task<IEnumerable<IGrouping<string, AttachmentDetailsDto>>> GetAttachmentsAsync(long assetInfoId)

within it I do...在里面我做...

var results = new List<AttachmentDetailsDto>();
var attachmentEntities = await _attachmentRepository.GetAsync(x => x.ParentAssetInfoId == assetInfoId && !x.IsDeleted).ConfigureAwait(false);

foreach (var attachmentEntity in attachmentEntities)
{
    var attachmentTypeEntity = await _attachmentTypeRepository.GetByIdAsync(attachmentEntity.MetattachTypeId).ConfigureAwait(false);

    var metattachDetail = new AttachmentDetailsDto
    {
        Id = attachmentEntity.Id,
        TypeId = attachmentEntity.MetattachTypeId,
        Type = attachmentTypeEntity.Name,
        Name = attachmentEntity.Name,
        Notes = attachmentEntity.Notes,
    };

    attachmentType = attachmentEntity.MetaType;
    var assetInfoEntity = default(AssetInfo);

    switch (attachmentType)
    {
        case "DigiDoc":
            var digiDocEntity = await _digiDocRepository.GetByIdAsync(attachmentEntity.TypeId).ConfigureAwait(false);
            metattachDetail.DigiDoc = new DigiDocDto
            {
                Id = digiDocEntity.Id,
                ContentType = digiDocEntity.ContentType,
                Url = digiDocEntity.Url
            };
            break;
... other cases ...
    }
    results.Add(metattachDetail);
}

return results.GroupBy(x => x.Type);

No errors here, but in my controller...这里没有错误,但在我的控制器中......

        public async Task<ActionResult> GetAttachmentsAsync(long assetInfoId)
        {
            var result = await _attachmentManager.GetAttachmentsAsync(assetInfoId).ConfigureAwait(false);
            return Ok(result);
        }

An error is thrown on line " return Ok(result); "在线抛出错误“ return Ok(result);

... I don't understand it, same code in .Net Framework works perfectly, now in .Net Core 3.1 it errors... ...我不明白,.Net Framework 中的相同代码运行良好,现在在 .Net Core 3.1 中出现错误...

Error:错误:

Unable to cast object of type 'AttachmentDetailsDto' to type 'System.String'.无法将“AttachmentDetailsDto”类型的对象转换为“System.String”类型。

Stack Trace:堆栈跟踪:

at System.Text.Json.JsonPropertyInfoNotNullable'4.OnWriteEnumerable(WriteStackFrame& current, Utf8JsonWriter writer) at System.Text.Json.JsonPropertyInfo.WriteEnumerable(WriteStack& state, Utf8JsonWriter writer) at System.Text.Json.JsonSerializer.HandleEnumerable(JsonClassInfo elementClassInfo, JsonSerializerOptions options, Utf8JsonWriter writer, WriteStack& state) at System.Text.Json.JsonSerializer.Write(Utf8JsonWriter writer, Int32 originalWriterDepth, Int32 flushThreshold, JsonSerializerOptions options, WriteStack& state) at System.Text.Json.JsonSerializer.d__50.MoveNext() at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.AspNetCore.Mvc.Formatters.SystemTextJsonOutputFormatter.d__5.MoveNext() at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at Microsoft.AspNetCore.在 System.Text.Json.JsonPropertyInfoNotNullable'4.OnWriteEnumerable(WriteStackFrame& current, Utf8JsonWriter writer) 在 System.Text.Json.JsonPropertyInfo.WriteEnumerable(WriteStack& state, Utf8JsonWriter writer) 在 System.Text.Json.JsonSerializer.JsonInfoClass 元素System.Text.Json.JsonSerializer.Write(Utf8JsonWriter writer, Int32 originalWriterDepth, Int32 flushThreshold, JsonSerializerOptions options, WriteStack& state) at System.Text.Json.JsonSerializer.d__50.Mount 的 JsonSerializerOptions 选项、Utf8JsonWriter 编写器、WriteStack& 状态)。 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.AspNetCore.Mvc.Formatters.SystemTextJsonOutputFormatter。 d__5.MoveNext() 在 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() 在 Microsoft.AspNetCore。 Mvc.Formatters.SystemTextJsonOutputFormatter.d__5.MoveNext() at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<g__Logged|21_0>d.MoveNext() at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<g__Awaited|29_0>d`2.MoveNext() at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResultExecutedContextSealed context) at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.ResultNext[TFilter,TFilterAsync](State& next, Sco Mvc.Formatters.SystemTextJsonOutputFormatter.d__5.MoveNext() 在 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() 在 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) 在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) ) 在 Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<g__Logged|21_0>d.MoveNext() 在 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() 在 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) 在 System .Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<g__Awaited|29_0>d`2.MoveNext() at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() 在微软。 AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResultExecutedContextSealed context) at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.ResultNext[TFilter,TFilterAsync](State& next, Sco pe& scope, Object& state, Boolean& isCompleted) at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeResultFilters() at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<g__Awaited|24_0>d.MoveNext() at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResourceExecutedContextSealed context) at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted) at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeFilterPipelineAsync() at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.R pe& 范围、对象&状态、Boolean& isCompleted) at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeResultFilters() at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<g__Awaited|24_0>d.MoveNext() at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at Microsoft.AspNetCore .Mvc.Infrastructure.ResourceInvoker.Rethrow(ResourceExecutedContextSealed context) at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted) at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeFilterPipelineAsync ) 在 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() 在 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) 在 System.R untime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<g__Logged|17_1>d.MoveNext() at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.AspNetCore.Routing.EndpointMiddleware.<g__AwaitRequestTask|6_0>d.MoveNext() at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.d__5.MoveNext() at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerService untime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) 在 Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<g__Logged|17_1>d.MoveNext() 在 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() 在 System.Runtime.CompilerServices .TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.AspNetCore.Routing.EndpointMiddleware.<g__AwaitRequestInfo|6_0>d.MoveNext() at System.Runtime.ExceptionServices.ExceptionDispatch Throw() at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.d__5.MoveNext() at System.Runtime。 ExceptionServices.ExceptionDispatchInfo.Throw() 在 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) 在 System.Runtime.CompilerService s.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.d__6.MoveNext() at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Swashbuckle.AspNetCore.SwaggerUI.SwaggerUIMiddleware.d__5.MoveNext() at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.d__4.MoveNext() at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Tas s.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.d__6.MoveNext() at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Swashbuckle.AspNetCore.SwaggerUI.SwaggerUIMiddleware.d__5.MoveNext() at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.Throw()任务任务) 在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) 在 Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.d__4.MoveNext() 在 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() 在 System.Runtime.CompilerServices。 TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Tas k task) at HomeaZZon.Api.Middlewares.ExceptionMiddleware.d__3.MoveNext() in C:\\repo\\homeazzon\\api\\api-.core-monolothic\\HomeaZZon.Api\\Middlewares\\ExceptionMiddleware.cs:line 28 k 任务)在 HomeaZZon.Api.Middlewares.ExceptionMiddleware.d__3.MoveNext() 在 C:\\repo\\homeazzon\\api\\api-.core-monolothic\\HomeaZZon.Api\\Middlewares\\ExceptionMiddleware.cs:line 28

... ...

Startup.cs启动文件

... ...

            services.AddMvc().AddJsonOptions(o =>
            {
                o.JsonSerializerOptions.IgnoreNullValues = true;
                o.JsonSerializerOptions.PropertyNamingPolicy = null;
            });

As someone else mentioned, it's hard to really nail down your error but if you simply want the same functionality you had in .NET Framework in terms of serialization, then here's how you do it.正如其他人所提到的,很难真正确定您的错误,但如果您只是想要在序列化方面与 .NET Framework 中的功能相同,那么您可以这样做。

Install the following nuget package :安装以下 nuget 包:

Install-Package Microsoft.AspNetCore.Mvc.NewtonsoftJson 

Then in your services method you'll have something like "AddMVC".然后在您的服务方法中,您将拥有类似“AddMVC”的内容。 Modify that like so :像这样修改:

services.AddMvc().AddNewtonsoftJson();

More info : https://dotnetcoretutorials.com/2019/12/19/using-newtonsoft-json-in-net-core-3-projects/更多信息: https : //dotnetcoretutorials.com/2019/12/19/using-newtonsoft-json-in-net-core-3-projects/

.NET Core projects 3+ use a new JSON serializer that honestly isn't bad, but has a couple of issues here and there. .NET Core 项目 3+ 使用了一个新的 JSON 序列化器,老实说还不错,但这里和那里有一些问题。 The performance benefits in later versions are a pretty big boon, but if you just need things to be exactly like they were before, then you need to use JSON.NET (With the above instructions), and you'll be back to old times!更高版本的性能优势是一个很大的好处,但是如果您只需要和以前完全一样,那么您需要使用 JSON.NET(按照上述说明),您将回到过去!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法将类型“ System.String”强制转换为类型“ System.Object” - Unable to cast the type 'System.String' to type 'System.Object' 无法将“DapperRow”类型的对象转换为“System.String”类型 - Unable to cast object of type 'DapperRow' to type 'System.String' 无法将“System.string”类型的对象转换为“....Event”类型 - Unable to cast object of type 'System.string' to type '....Event' 无法将类型为“ System.String”的对象转换为类型为“ FileItem”的对象 - Unable to cast object of type 'System.String' to type 'FileItem' 无法将类型类的对象强制转换为“ System.String”类型 - Unable to cast object of type class to type 'System.String' 无法将“System.String”类型的对象转换为“System.Collections.Generic.List`1[System.String]”类型 - Unable to cast object of type 'System.String' to type 'System.Collections.Generic.List`1[System.String]' C# 中的 MS Word 自动化 - 无法将类型为“System.String[*]”的 object 转换为类型“System.String[]” - MS Word Automation in C# - Unable to cast object of type 'System.String[*]' to type 'System.String[]' 无法将对象类型“ System.String [*]”转换为类型“ System.String []” - Unable To Cast object type 'System.String[*]' to type 'System.String[]' 无法将类型为“ TextmlBatchDeleteDocument”的对象转换为“ System.String” - Unable to cast object of type 'TextmlBatchDeleteDocument' to 'System.String' 如何解决“无法在CallBimlScript中强制转换&#39;System.String&#39;类型的对象” - How to troubleshoot “Unable to cast object of type 'System.String'” in CallBimlScript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM