title: ASP.NET Core Deserializing From Body, w/ Default Values
body: <p>I have the following controller (abbreviated):</p>
  
  <pre><code>public class ServicesController : Controller
  {
      private readonly IServiceRepository _repo;
      public ServicesController(IServiceRepository repo)
      {
          _repo = repo;
      }
  
      [HttpPost]
      public async Task&lt;StatusCodeResult&gt; CreateService([FromBody] JsonServiceEntity service)
      {
          return await _repo.CreateServiceAsync(new ServiceEntity(service));
      }
  }
  </code></pre>
  
  <p>The JsonServiceEntity class looks like this (abbreviated):</p>
  
  <pre><code>public class JsonServiceEntity
  {
      public string Id { get; set; } = Guid.NewGuid().ToString();
  }
  </code></pre>
  
  <p><code>Id</code> is a string in the form of a guid. If one is passed in from the client, I should use the given Id. If no value is passed in from the client, I should generate a new guid and use that.</p>
  
  <p>However, I can't get it to use the default value when none is given from the client. No matter what, it ends up as an empty string. I can debug and watch it hit the property and generate a new guid for the default, which is promptly overwritten by a call to the setter with an empty string.</p>
  
  <p>I've tried different values of <code>NullValueHandling</code> and <code>DefaultValueHandling</code> on the <code>SerializerSettings</code> in my <code>Startup.cs</code>, but that hasn't helped. If I set <code>[Required]</code> on the <code>Id</code> property, I get a ModelState error letting me know that the attempted value was <code>null</code>, not an empty string.</p>
  
  <p>How do I get it to take the default value?</p>
  
  <pre><code>CONNECTServiceRegistry.dll!CONNECTServiceRegistry.Models.JsonServiceEntity.Id.set(string value) Line 45 C#
  Newtonsoft.Json.dll!Newtonsoft.Json.Serialization.ExpressionValueProvider.SetValue(object target, object value) Unknown
  Newtonsoft.Json.dll!Newtonsoft.Json.Serialization.JsonSerializerInternalReader.SetPropertyValue(Newtonsoft.Json.Serialization.JsonProperty property, Newtonsoft.Json.JsonConverter propertyConverter, Newtonsoft.Json.Serialization.JsonContainerContract containerContract, Newtonsoft.Json.Serialization.JsonProperty containerProperty, Newtonsoft.Json.JsonReader reader, object target)    Unknown
  Newtonsoft.Json.dll!Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateObject(object newObject, Newtonsoft.Json.JsonReader reader, Newtonsoft.Json.Serialization.JsonObjectContract contract, Newtonsoft.Json.Serialization.JsonProperty member, string id) Unknown
  Newtonsoft.Json.dll!Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject(Newtonsoft.Json.JsonReader reader, System.Type objectType, Newtonsoft.Json.Serialization.JsonContract contract, Newtonsoft.Json.Serialization.JsonProperty member, Newtonsoft.Json.Serialization.JsonContainerContract containerContract, Newtonsoft.Json.Serialization.JsonProperty containerMember, object existingValue) Unknown
  Newtonsoft.Json.dll!Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(Newtonsoft.Json.JsonReader reader, System.Type objectType, Newtonsoft.Json.Serialization.JsonContract contract, Newtonsoft.Json.Serialization.JsonProperty member, Newtonsoft.Json.Serialization.JsonContainerContract containerContract, Newtonsoft.Json.Serialization.JsonProperty containerMember, object existingValue)  Unknown
  Newtonsoft.Json.dll!Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(Newtonsoft.Json.JsonReader reader, System.Type objectType, bool checkAdditionalContent)  Unknown
  Newtonsoft.Json.dll!Newtonsoft.Json.JsonSerializer.DeserializeInternal(Newtonsoft.Json.JsonReader reader, System.Type objectType)   Unknown
  Newtonsoft.Json.dll!Newtonsoft.Json.JsonSerializer.Deserialize(Newtonsoft.Json.JsonReader reader, System.Type objectType)   Unknown
  Microsoft.AspNetCore.Mvc.Formatters.Json.dll!Microsoft.AspNetCore.Mvc.Formatters.JsonInputFormatter.ReadRequestBodyAsync(Microsoft.AspNetCore.Mvc.Formatters.InputFormatterContext context, System.Text.Encoding encoding) Line 303 C#
  Microsoft.AspNetCore.Mvc.Core.dll!Microsoft.AspNetCore.Mvc.Formatters.TextInputFormatter.ReadRequestBodyAsync(Microsoft.AspNetCore.Mvc.Formatters.InputFormatterContext context) Line 57    C#
  Microsoft.AspNetCore.Mvc.Core.dll!Microsoft.AspNetCore.Mvc.Formatters.InputFormatter.ReadAsync(Microsoft.AspNetCore.Mvc.Formatters.InputFormatterContext context) Line 116  C#
  Microsoft.AspNetCore.Mvc.Core.dll!Microsoft.AspNetCore.Mvc.ModelBinding.Binders.BodyModelBinder.BindModelAsync(Microsoft.AspNetCore.Mvc.ModelBinding.ModelBindingContext bindingContext) Line 158   C#
  Microsoft.AspNetCore.Mvc.Core.dll!Microsoft.AspNetCore.Mvc.ModelBinding.ParameterBinder.BindModelAsync(Microsoft.AspNetCore.Mvc.ActionContext actionContext, Microsoft.AspNetCore.Mvc.ModelBinding.IModelBinder modelBinder, Microsoft.AspNetCore.Mvc.ModelBinding.IValueProvider valueProvider, Microsoft.AspNetCore.Mvc.Abstractions.ParameterDescriptor parameter, Microsoft.AspNetCore.Mvc.ModelBinding.ModelMetadata metadata, object value) Line 239  C#
  Microsoft.AspNetCore.Mvc.Core.dll!Microsoft.AspNetCore.Mvc.Internal.ControllerBinderDelegateProvider.CreateBinderDelegate.__Bind|0(Microsoft.AspNetCore.Mvc.ControllerContext controllerContext, object controller, System.Collections.Generic.Dictionary&lt;string, object&gt; arguments) Line 77    C#
  Microsoft.AspNetCore.Mvc.Core.dll!Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.BindArgumentsAsync() Line 424   C#
  Microsoft.AspNetCore.Mvc.Core.dll!Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Next(ref Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.State next, ref Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Scope scope, ref object state, ref bool isCompleted) Line 69   C#
  Microsoft.AspNetCore.Mvc.Core.dll!Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeInnerFilterAsync() Line 385   C#
  Microsoft.AspNetCore.Mvc.Core.dll!Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Next(ref Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.State next, ref Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Scope scope, ref object state, ref bool isCompleted) Line 692  C#
  Microsoft.AspNetCore.Mvc.Core.dll!Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeNextResourceFilter() Line 793 C#
  Microsoft.AspNetCore.Mvc.Core.dll!Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Next(ref Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.State next, ref Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Scope scope, ref object state, ref bool isCompleted) Line 407  C#
  Microsoft.AspNetCore.Mvc.Core.dll!Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeFilterPipelineAsync() Line 123    C#
  Microsoft.AspNetCore.Mvc.Core.dll!Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeAsync() Line 81   C#
  Microsoft.AspNetCore.Mvc.Core.dll!Microsoft.AspNetCore.Mvc.Internal.MvcAttributeRouteHandler.RouteAsync.AnonymousMethod__0(Microsoft.AspNetCore.Http.HttpContext c) Line 105    C#
  </code></pre>