<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Christian Wattengård</title>
    <description>The latest articles on DEV Community by Christian Wattengård (@wattengard).</description>
    <link>https://dev.to/wattengard</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F270876%2F02b0ed12-b4e5-4f33-8630-d56f7aea9c7c.jpeg</url>
      <title>DEV Community: Christian Wattengård</title>
      <link>https://dev.to/wattengard</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/wattengard"/>
    <language>en</language>
    <item>
      <title>Why your POSTed models may stop working after upgrading to ASP.NET Core 3.1</title>
      <dc:creator>Christian Wattengård</dc:creator>
      <pubDate>Tue, 14 Jan 2020 09:44:23 +0000</pubDate>
      <link>https://dev.to/wattengard/why-your-posted-models-may-stop-working-after-upgrading-to-asp-net-core-3-1-4ekp</link>
      <guid>https://dev.to/wattengard/why-your-posted-models-may-stop-working-after-upgrading-to-asp-net-core-3-1-4ekp</guid>
      <description>&lt;p&gt;I just upgraded my medium size application from ASP.NET Core 2.2 to 3.1 and everything seemed to work fine. The upgrade wasn't a very big task but there were some gotchas along the way. One of the gotchas that went unnoticed until I was trying to get a new feature working was particularly hard to debug.&lt;/p&gt;

&lt;p&gt;Starting with ASP.NET Core 3.0 the model-binder handling incoming JSON-data uses &lt;br&gt;
&lt;code&gt;System.Text.Json&lt;/code&gt; instead of Json.NET. This shouldn't immediately be any issue, but I am lazy and pragmatic, so when I for reasons want to post my forms using ajax, I iterate over all input-fields and generate a JSON-structure that I POST to my endpoint. HTML being what it is, all integer inputs return their values as strings, and this is what blows up. &lt;/p&gt;

&lt;p&gt;Previously the Json.NET parser said "Oh hi, I need an int here, but the incoming value is a string... Hmm.. Is it parsable though? Yes it is! OK, I'll stuff the parsed value into the int property then!". But now, the &lt;code&gt;System.Text.Json&lt;/code&gt; parser doesn't do that. It says "Oh no! I need an int here! But the incoming value is a string! OMG! ABANDON SHIP! AAAAAAaaaaaaaa!!!" and throws away the whole model, giving you a big fat NULL in your incoming parameter. And failing silently of course. To find out what's going on you need to inspect the &lt;code&gt;ModelState&lt;/code&gt;, and sure enough, the &lt;code&gt;ModelState&lt;/code&gt; reports an error parsing a Int32 from the provided value "0".&lt;/p&gt;

&lt;p&gt;The fix, not including actually posting the correct model to begin with (lazy, remember) is adding the package &lt;code&gt;Microsoft.AspNetCore.Mvc.NewtonsoftJson&lt;/code&gt; and updating your configuration in &lt;code&gt;Startup.cs&lt;/code&gt; to include &lt;code&gt;.AddNewtonsoftJson()&lt;/code&gt; after &lt;code&gt;.AddMvc()&lt;/code&gt; or &lt;code&gt;.AddControllers()&lt;/code&gt; or whatever method you use to add your api-functionality. This will replace the new System.Text.Json parser with the old true and tried Newtonsoft Json.NET parser.&lt;/p&gt;

&lt;p&gt;If you want to read more about the new &lt;code&gt;System.Text.Json&lt;/code&gt; namespace you can go &lt;a href="https://devblogs.microsoft.com/dotnet/try-the-new-system-text-json-apis/"&gt;here&lt;/a&gt;. If you want to read the blog post that I found after screwing around with this for half a work day, it's &lt;a href="https://dotnetcoretutorials.com/2019/12/19/using-newtonsoft-json-in-net-core-3-projects/"&gt;here&lt;/a&gt;&lt;/p&gt;

</description>
      <category>dotnet</category>
      <category>aspnet</category>
    </item>
  </channel>
</rss>
