简体   繁体   中英

.NET Core 3.1/React Template - REST API Class - Ignore Property in JSON result

I have a project on C# Core 3.1 with React and Redux template with EF Core for DB. I need to exclude Id from the DTO class which I send via REST API Controller.

I need to use Id for load-related class vie EF Core, but I do not show in REST API result:

{
    "id": 1, -- DISABLE to show
    "product": "TEST1"
}

C# Class:

public class ProductResDto
{
    public int Id { get; set; } // DISABLE to show in REST API result
    public string Product{ get; set; }
}

Controller:

    // GET api/<ProductController>
    [HttpPost]
    public async Task<IActionResult> Post(ProductReqDto model)
    {
        try
        {
            var product = await _context.Products
                .OrderBy(b => b.Name)
                .Where(w => w.Name == model.Name)
            .Select(c => new ProductResDto
            {
                Id = c.Id,
                Product = c.Name                    
            }).FirstAsync();

            // Grades
            product.Grades = ''; // LOAD ADD. INFO (use product.Id like FK in DB)

            // Description
            product.Descriptions = ''; // LOAD ADD. INFO (use product.Id like FK in DB)

            // Files
            product.Files = ''; // LOAD ADD. INFO (use product.Id like FK in DB)

            // Parameters
            product.Parameters = ''; // LOAD ADD. INFO (use product.Id like FK in DB)

            return Ok(product);
        }
        catch (Exception e)
        {
            return NotFound();
        }
    }

Is there any chance to this parameter disable for REST API result or load in await _context.Products using by .Select()

2 objects: One for ID only and one for ProductResDto

Thank you

Update your class as following

public class ProductResDto
{
    [System.Text.Json.Serialization.JsonIgnore]
    public int Id { get; set; } 
    public string Product { get; set; }
}

I see different solutions:

  1. Using JsonIgnoreAttribute on Id property like this:

     using System.Text.Json.Serialization; public class ProductResDto { [JsonIgnore] public int Id { get; set; } public string Product { get; set; } }
  2. Create Contract -layer for your application that will contain all the OutCountract models. So your Contract model will not contain Id property:

     public class ProductResOutContract { public string Product { get; set; } }

    And utilize it like:

     // GET api/<ProductController> [HttpPost] public async Task<IActionResult> Post(ProductReqDto model) { try { var product = await _context.Products .OrderBy(b => b.Name) .Where(w => w.Name == model.Name) .Select(c => new ProductResDto { Id = c.Id, Product = c.Name }).FirstAsync(); // Grades product.Grades = ''; // LOAD ADD. INFO (use product.Id like FK in DB) // Description product.Descriptions = ''; // LOAD ADD. INFO (use product.Id like FK in DB) // Files product.Files = ''; // LOAD ADD. INFO (use product.Id like FK in DB) // Parameters product.Parameters = ''; // LOAD ADD. INFO (use product.Id like FK in DB) var productOutContract = ConvertToOutCountract(product); return Ok(productOutContract); } catch (Exception e) { return NotFound(); } } private static ProductReqOutContract ConvertToOutCountract(ProductReqDto product) { var outCountract = new ProductOutContract { // Fill the properties; }; return outCountract; }
  3. Returning Id is also not bad. You should just handle the model on the frontend properly.

I prefer the 2nd or the 3rd solution. It's more clear when the API-layer and data-layer are separate.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM