.NET Core 3 [JsonIgnore]在请求单个资源时不工作

xytpbqjk  于 2023-05-08  发布在  .NET
关注(0)|答案(7)|浏览(119)

在我的.net Core 3.0 API中,[JsonIgnore]属性不正常工作。我使用System.Text.Json
而不是旧的Newtonsoft.Json
当我使用返回对象列表的资源时,例如:

/api/Object/

对象序列化如下:

[
      {
        "id": 1,
        "date": "2020-02-12T08:45:51.502",
        "userId": 1,
        "tags": [
          {
            "name": "string"
          }
        ]
      }
    ]

但是当我请求一个结果时

/api/Object/{id}

完整的对象被序列化如下:

{
  "user": {
    "hasAccess": false,
    "id": 1,
    "userName": "***",
    "normalizedUserName": "***",
    "email": "***",
    "normalizedEmail": "***",
    "emailConfirmed": true,
    "passwordHash": "***",
    "concurrencyStamp": "***",
    "phoneNumberConfirmed": false,
    "twoFactorEnabled": false,
    "lockoutEnabled": true,
    "accessFailedCount": 0
  },
  "lazyLoader": {},
  "id": 1,
  "date": "2020-02-12T08:45:51.502",
  "userId": 1,
  "tags": [
    {
      "name": "string"
    }
  ]
}

带有JsonIgnore属性的类看起来像这样:

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text.Json.Serialization;
 public class Object
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }
    public DateTime Date { get; set; }
    [ForeignKey("User")]
    public int UserId { get; set; }
    [JsonIgnore]
    public virtual User User { get; set; }
}

这是我的WebApi控制器类:

using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Models;
using Services;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
  
  [Route("api/object")]
    [ApiController]
    [Authorize(Roles = Roles.ACCESS_GRANTED)]
    public class ObjectController : AbstractController
    {
        private readonly ObjectService objectService;

        public ObjectController(IDataService<Object> service, ExtendedUserManager manager) : base(manager)
        {
            objectService = (ObjectService)service;
        }

        // GET: api/Object
        [HttpGet]
        public IActionResult Get()
        {
            List<Object> object = objectService.GetAll();
            return Ok(object);
        }

        // GET: api/Object/5
        [HttpGet("{id}", Name = "GetObject")]
        public IActionResult Get(int id)
        {
            Object object = objectService.Get(id);

            if (object == null)
            {
                return NotFound(string.Format("Object with Id {0} could not be found", id));
            }

            return Ok(object);
    }
}

我的csproj文件:

<Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup>
    <TargetFramework>netcoreapp3.1</TargetFramework>
    <TypeScriptCompileBlocked>true</TypeScriptCompileBlocked>
    <TypeScriptToolsVersion>Latest</TypeScriptToolsVersion>
    <IsPackable>false</IsPackable>
    <SpaRoot>ClientApp\</SpaRoot>
    <DefaultItemExcludes>$(DefaultItemExcludes);$(SpaRoot)node_modules\**</DefaultItemExcludes>

    <!-- Set this to true if you enable server-side prerendering -->
    <BuildServerSideRenderer>false</BuildServerSideRenderer>
    <RootNamespace>Project</RootNamespace>
    <Configurations>Debug;Release;</Configurations>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="7.0.0" />
    <PackageReference Include="log4net" Version="2.0.8" />
    <PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="3.1.1" />
    <PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="3.1.1" />
    <PackageReference Include="Microsoft.AspNetCore.SpaServices.Extensions" Version="3.1.1" />
    <PackageReference Include="Microsoft.EntityFrameworkCore" Version="3.1.1" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Proxies" Version="3.1.1" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="3.1.1" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.1.1" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="3.1.1">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </PackageReference>
    <PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="3.1.1" />
    <PackageReference Include="Microsoft.Extensions.Logging.Log4Net.AspNetCore" Version="3.0.3" />
    <PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="3.1.1" />
    <PackageReference Include="SendGrid" Version="9.12.6" />
    <PackageReference Include="Swashbuckle.AspNetCore" Version="5.0.0" />
    <PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="5.6.0" />
  </ItemGroup>

  <ItemGroup>
    <!-- Don't publish the SPA source files, but do show them in the project files list -->
    <Content Remove="$(SpaRoot)**" />
    <None Remove="$(SpaRoot)**" />
    <None Include="$(SpaRoot)**" Exclude="$(SpaRoot)node_modules\**" />
  </ItemGroup>

  <Target Name="Restore">
    <MSBuild Projects="$.\open-success.sln" Targets="Restore" />
  </Target>

  <Target Name="DebugEnsureNodeEnv" BeforeTargets="Build" Condition=" '$(Configuration)' == 'Debug' And !Exists('$(SpaRoot)node_modules') ">
    <!-- Ensure Node.js is installed -->
    <Exec Command="node --version" ContinueOnError="true">
      <Output TaskParameter="ExitCode" PropertyName="ErrorCode" />
    </Exec>
    <Error Condition="'$(ErrorCode)' != '0'" Text="Node.js is required to build and run this project. To continue, please install Node.js from https://nodejs.org/, and then restart your command prompt or IDE." />
    <Message Importance="high" Text="Restoring dependencies using 'npm'. This may take several minutes..." />
    <Exec WorkingDirectory="$(SpaRoot)" Command="npm install" />
  </Target>

  <Target Name="PublishRunWebpack" AfterTargets="ComputeFilesToPublish">
    <!-- As part of publishing, ensure the JS resources are freshly built in production mode -->
    <Exec WorkingDirectory="$(SpaRoot)" Command="npm install" />
 
    <!-- Include the newly-built files in the publish output -->
    <ItemGroup>
      <DistFiles Include="$(SpaRoot)dist\**; $(SpaRoot)dist-server\**" />
      <DistFiles Include="$(SpaRoot)node_modules\**" Condition="'$(BuildServerSideRenderer)' == 'true'" />
      <ResolvedFileToPublish Include="@(DistFiles->'%(FullPath)')" Exclude="@(ResolvedFileToPublish)">
        <RelativePath>%(DistFiles.Identity)</RelativePath>
        <CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
        <ExcludeFromSingleFile>true</ExcludeFromSingleFile>
      </ResolvedFileToPublish>
    </ItemGroup>
  </Target>

</Project>

我的创业:

public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddCors();
            services.AddControllers();
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            app.UseCors(builder => builder.AllowAnyHeader().AllowAnyOrigin().WithMethods("*"));
            app.UseDeveloperExceptionPage();
            app.UseHttpsRedirection();
            app.UseRouting();
            app.UseAuthorization();
            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllerRoute(
                    name: "default",
                    pattern: "{controller}/{action=Index}/{id?}");
            });
        }
    }

是我错过了什么还是这是一个Bug?

更新:

我注意到另一个奇怪的行为,当我返回一个新创建的Object而不是数据库中的对象时,一切都工作得很好。

[HttpGet("{id}", Name = "GetObject")]
    public IActionResult Get(int id)
    {
        // Object object= objectService.Get(id);
        Object object= new Object ();
        object.User = new User();

        if (object== null)
        {
            return NotFound(string.Format("object with Id {0} could not be found", id));
        }

        return Ok(object);
    }
b1uwtaje

b1uwtaje1#

您需要:

using Newtonsoft.Json;

而不是:

using System.Text.Json.Serialization;

nuget包是Microsoft.AspNetCore.Mvc.NewtonsoftJson。它不再包含在.net core中。

k2arahey

k2arahey2#

您可以从System.Text.Json使用[JsonIgnore],如果您手动调用序列化/反序列化方法,它将工作。但是,内置的.net核心序列化系统(即对于控制器,httpclient.GetFromJsonAsync..等。[JsonIgnore]属性到目前为止不起作用。
看起来内部序列化不使用System.Text.Json,并且JsonIgnore属性尚未调整。在这种情况下,使用[IgnoreDataMember]属性和.net核心内部系统将忽略这些属性,它将正常工作。

3bygqnnd

3bygqnnd3#

我刚刚提交了一份bug report about this。在System.Text.JSON中使用延迟加载代理时会发生此问题。
解决方法(除非您想切换到Newtonsoft)是手动调用序列化程序并返回Content()。例如:

[HttpGet("{id}")]
public ActionResult Get(int id)
{
    var result = _context.Table.Find(id);
    return Content(JsonSerializer.Serialize(result), "application/json");
}

不要忘记在Startup中提供您通过AddJsonOptions配置的相同序列化器选项(如果有的话)

bxgwgixi

bxgwgixi4#

您需要在Startup.cs中的ConfigureServices方法中添加以下代码

services.AddControllers()
                    .AddNewtonsoftJson(options =>
                    {
                        options.SerializerSettings.ContractResolver = 
                                                     new DefaultContractResolver();
                    });

当然,您需要Microsoft.AspNetCore.Mvc.NewtonsoftJson

omhiaaxx

omhiaaxx5#

对我来说,这个问题是由lazyLoadingProxies引起的。从数据库加载的用户不是User类型,而是Castle.Proxies.UserProxy类型,其中没有应用JsonIgnore属性。我看到3个解决方案
1.使用ILazyLoader而不是代理,如下所述:https://www.learnentityframeworkcore.com/lazy-loading
1.始终Map延迟加载的属性
1.完全不要使用延迟加载(最好不要在EF Core中使用它)

gzszwxb4

gzszwxb46#

我正在使用.NetCore 3.1System.Text.Json.Serialization v6.0.0.0
[JsonIgnore]对我来说很好

mqxuamgl

mqxuamgl7#

对我来说,我发现如果你返回对象而不是显式的JsonResult,它会忽略JsonIngore属性,你会得到一个异常。如果将返回类型更改为JsonResult,它将遵守该属性。
抛出异常-

[HttpGet]
public async Task<PurchaseOrder> Order(string orderNumber)

Respects [JsonIgnore]属性-

[HttpGet]
public async Task<JsonResult> Order(string orderNumber)

相关问题