locked
Partial Update, JsonPatchDocument gets null RRS feed

  • Question

  • User-309523270 posted

    Hi,

    I'm having problems passing the patch document to the JsonPatchDocument variable patchDoc for Patch.  patchDoc gets null.  I test using postman.

    Code is as follows:

    CodeTableController.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Threading.Tasks;
    using Microsoft.AspNetCore.Mvc;
    using myCMS.Business;
    using myCMS.Business.Repository;
    using Common;
    using Common.DTOs;
    using Common.Models ;
    using Common.Entities;
    using AutoMapper;
    using Microsoft.AspNetCore.JsonPatch;

    namespace myCMSNet.WebApi.Controllers
    {

        [Route("api/[controller]")]
        public class CodeTableController : Controller
        {
            private readonly CodeTableRepository _CodeTableRepository;
            private readonly IMapper _mapper;

            public CodeTableController(CodeTableRepository aCodeTableRepository, IMapper mapper)
            {
                _CodeTableRepository = aCodeTableRepository;
                _mapper = mapper;
            }

            [HttpPatch("patch/{ascategory}/{ascode}")]
            public async Task<IActionResult> patch(string asCategory, string asCode, [FromBody] JsonPatchDocument<CodeTableAddVM> patchDoc )
            {

                    if (ModelState.IsValid)
                    {
                    }
             }
        }
    }
     

    public class CodeTableAddVM : BaseEntity
    {
    public string Category { get; set; }
    public string Code { get; set; }
    public string Value { get; set; }
    public int Rank { get; set; }
    public DateTime? inactive_dt { get; set; }
    }

    Postman Patch Call

    http://localhost:5000/api/CodeTable/patch/victim_category/TEST

    Header
    Content-type     :      I've  tried both - application/json  and application/json-patch+json

    Body
    raw - JSON(application/json) and/or Text
    { "op" : "replace", "path": "/Value", "value": "TEST ONE" }

    I appreciate your input!

    Thanks,

    tinac99

    Wednesday, March 14, 2018 12:46 AM

All replies

  • User-309523270 posted

    Do you think this could be factor?

    Output When Building The Project

    4>F:\Deployed\myCMS\CMSNet.WebApi\myCMSNet.WebApi\myCMSNet.WebApi\myCMSNet.WebApi.csproj : warning NU1701: Package 'JsonPatch 1.0.0' was restored using '.NETFramework,Version=v4.6.1' instead of the project target framework '.NETCoreApp,Version=v2.0'. This package may not be fully compatible with your project.

    4>F:\Deployed\myCMS\CMSNet.WebApi\myCMSNet.WebApi\myCMSNet.WebApi\myCMSNet.WebApi.csproj : warning NU1701: Package 'Microsoft.AspNet.WebApi.Core 5.2.4' was restored using '.NETFramework,Version=v4.6.1' instead of the project target framework '.NETCoreApp,Version=v2.0'. This package may not be fully compatible with your project.

    4>F:\Deployed\myCMS\CMSNet.WebApi\myCMSNet.WebApi\myCMSNet.WebApi\myCMSNet.WebApi.csproj : warning NU1701: Package 'Microsoft.AspNet.WebApi.OData 5.7.0' was restored using '.NETFramework,Version=v4.6.1' instead of the project target framework '.NETCoreApp,Version=v2.0'. This package may not be fully compatible with your project.

    4>F:\Deployed\myCMS\CMSNet.WebApi\myCMSNet.WebApi\myCMSNet.WebApi\myCMSNet.WebApi.csproj : warning NU1701: Package 'Microsoft.AspNet.WebApi.Tracing 5.2.4' was restored using '.NETFramework,Version=v4.6.1' instead of the project target framework '.NETCoreApp,Version=v2.0'. This package may not be fully compatible with your project.

    4>F:\Deployed\myCMS\CMSNet.WebApi\myCMSNet.WebApi\myCMSNet.WebApi\myCMSNet.WebApi.csproj : warning NU1701: Package 'Microsoft.AspNet.WebApi.WebHost 5.2.4' was restored using '.NETFramework,Version=v4.6.1' instead of the project target framework '.NETCoreApp,Version=v2.0'. This package may not be fully compatible with your project.

    Wednesday, March 14, 2018 6:21 PM
  • User283571144 posted

    Hi tinac99,

    Output When Building The Project

    4>F:\Deployed\myCMS\CMSNet.WebApi\myCMSNet.WebApi\myCMSNet.WebApi\myCMSNet.WebApi.csproj : warning NU1701: Package 'JsonPatch 1.0.0' was restored using '.NETFramework,Version=v4.6.1' instead of the project target framework '.NETCoreApp,Version=v2.0'. This package may not be fully compatible with your project.

    4>F:\Deployed\myCMS\CMSNet.WebApi\myCMSNet.WebApi\myCMSNet.WebApi\myCMSNet.WebApi.csproj : warning NU1701: Package 'Microsoft.AspNet.WebApi.Core 5.2.4' was restored using '.NETFramework,Version=v4.6.1' instead of the project target framework '.NETCoreApp,Version=v2.0'. This package may not be fully compatible with your project.

    4>F:\Deployed\myCMS\CMSNet.WebApi\myCMSNet.WebApi\myCMSNet.WebApi\myCMSNet.WebApi.csproj : warning NU1701: Package 'Microsoft.AspNet.WebApi.OData 5.7.0' was restored using '.NETFramework,Version=v4.6.1' instead of the project target framework '.NETCoreApp,Version=v2.0'. This package may not be fully compatible with your project.

    4>F:\Deployed\myCMS\CMSNet.WebApi\myCMSNet.WebApi\myCMSNet.WebApi\myCMSNet.WebApi.csproj : warning NU1701: Package 'Microsoft.AspNet.WebApi.Tracing 5.2.4' was restored using '.NETFramework,Version=v4.6.1' instead of the project target framework '.NETCoreApp,Version=v2.0'. This package may not be fully compatible with your project.

    4>F:\Deployed\myCMS\CMSNet.WebApi\myCMSNet.WebApi\myCMSNet.WebApi\myCMSNet.WebApi.csproj : warning NU1701: Package 'Microsoft.AspNet.WebApi.WebHost 5.2.4' was restored using '.NETFramework,Version=v4.6.1' instead of the project target framework '.NETCoreApp,Version=v2.0'. This package may not be fully compatible with your project.

    According to the error message, I guess your web application is net core 2.0, but you used the asp.net 4.6 package.

    I suggest you could firstly remove these package and reinstall the asp.net core 2.0 package.

    Package as below:

    Also I have craeted a test demo on the asp.net core web api, it works well.

    Codes as below:

        [Route("api/[controller]")]
        public class PersonController : Controller
        {
            private readonly Person _defaultPerson = new Person
            {
                FirstName = "Jim",
                LastName = "Smith"
            };
    
            [HttpPatch("update")]
            public Person Patch([FromBody]JsonPatchDocument<Person> personPatch)
            {
                personPatch.ApplyTo(_defaultPerson);
                return _defaultPerson;
            }
        }
    
        public class Person
        {
            public string FirstName { get; set; }
            public string LastName { get; set; }
        }

    Result:

    Best Regards,

    Brando

    Thursday, March 15, 2018 6:24 AM