Click here to Skip to main content
15,923,273 members

The Weird and The Wonderful

   

The Weird and The Wonderful forum is a place to post Coding Horrors, Worst Practices, and the occasional flash of brilliance.

We all come across code that simply boggles the mind. Lazy kludges, embarrassing mistakes, horrid workarounds and developers just not quite getting it. And then somedays we come across - or write - the truly sublime.

Post your Best, your worst, and your most interesting. But please - no programming questions . This forum is purely for amusement and discussions on code snippets. All actual programming questions will be removed.

 
GeneralRe: WTF-8 Pin
PIEBALDconsult18-Jun-24 14:07
mvePIEBALDconsult18-Jun-24 14:07 
GeneralRe: WTF-8 Pin
honey the codewitch18-Jun-24 6:01
mvahoney the codewitch18-Jun-24 6:01 
GeneralRe: WTF-8 Pin
PIEBALDconsult18-Jun-24 14:10
mvePIEBALDconsult18-Jun-24 14:10 
GeneralRe: WTF-8 Pin
honey the codewitch18-Jun-24 15:01
mvahoney the codewitch18-Jun-24 15:01 
GeneralRe: WTF-8 Pin
PIEBALDconsult18-Jun-24 18:14
mvePIEBALDconsult18-Jun-24 18:14 
GeneralRe: WTF-8 Pin
honey the codewitch18-Jun-24 22:43
mvahoney the codewitch18-Jun-24 22:43 
GeneralRe: WTF-8 Pin
PIEBALDconsult19-Jun-24 3:12
mvePIEBALDconsult19-Jun-24 3:12 
General.NET Core & (auto)binding: Is it a bug? Pin
raddevus16-Jun-24 5:15
mvaraddevus16-Jun-24 5:15 
My career seems to be entirely described by the following:

"Use a simple technology to do a thing that 80% of Devs take for granted and who also tell you it works, but discovering that the thing doesn't actually work the way they say it works."

Here's the latest. I will try to keep it short (because I'm going to write an article on it, but I can't wait to share this one because it is so weird (from multiple angles)).

1) JavaScript FormData Posted To Backend Is All Strings
Mabye a lot of you know this, but all of the values in the following (real) example end up being strings when they get to the backend.

JavaScript
//first notice that I'm appending two items to the formData.
// 1. string uuid
// 2. journalEntry object with numerous values

var formData = new FormData();
formData.append("uuid",currentUuid);

var entryId = 9;
var jentry = {
        Id:entryId,
        Title: titleText,
        Note: noteText,
        Created: createdDate,
        Updated: null
    };

// You have to do this weird thing to add the data to FormData.

 for (var key in jentry) {
        formData.append(key, jentry[key]);
   }
Odd, But Acceptable
Ok, so the point of that is that all of the values in the jentry object are converted to string values. That means Updated = "null" (string) and the Id = "9".
This is odd, to me, but acceptable.
If you refute this, please provide citations. I've searched far & wide and asked Copilot -- all says they are strings when they hit the backend.

Now It Gets Real Weird
My WebAPI method which gets called by the JS Fetch with the previously shown FormData looks like:

C#
public ActionResult Save([FromForm] String uuid,[FromForm] JournalEntry jentry)


AutoBinding In .NET Core Works With JS Fetch
1. The JS Fetch works.
2. The C# JournalEntry is created from the data posted via the FormData variables.
3. I can examine the data in the C# JournalEntry object and (almost) everything looks fine.

Autobinding Ignores The Id Value!
However, at some point I needed the Id value which was being passed in.
But I noticed that the Id value in the C# object was always ZERO Id = 0.
FormData Value Is Non-Zero, But Autobound Object Is 0
But, keep in mind that the FormData object shown above is passing in a non-zero value for Id (9 in the example.

What!?!

Why Is This Happening?

The only thing I could guess is that since the Id value (from the FormData) was being passed as a String that the autobinding just ignores the value and sets it to 0.

Here's How My C# Object (Used for Autobinding) Is Defined

C#
public class JournalEntry{
    public Int64 Id{get;set;}
    public String? Title{get;set;}
    public String Note{get;set;}
    public String Created{get;set;}
    public String? Updated{get;set;}
}


Yes, the point is that the Id is an Int64 (not a string).

This Part Should Alarm You!!! This Should Be The Bug
So, somehow the autobinder is able to bind the JournalEntry to the FormData object but it just ignores the Id value and sets it to 0!!!!

Why?? !! Please someone explain it to me.

A "Workaround" Test
I altered my JournalEntry class so the Id is a String
C#
public class JournalEntry{
    public String Id{get;set;}
   // ....snip....


and posted the data again.

Autobinder Works! What?!
After that change, the autobinder sets the Id value properly (non-zero value that is read from FormData).

Not What I Expected
This is not what I expected. JSON data is _supposed_ to get autobound to the C# object, right?

Just Binds the Parts It Wants To Bind?
It's weird because it doesn't fail to bind it just skips parts of the object that it wants to skip.
I could accept it if it failed to bind because it was the wrong type.

Article Is Coming Later Today
This was long but I will be writing it up in an article with associated examples and an example project you'll be able to download and try for yourself.

modified 17-Jun-24 8:08am.

GeneralRe: .NET Core & (auto)binding: Is it a bug? Pin
0x01AA16-Jun-24 5:58
mve0x01AA16-Jun-24 5:58 
GeneralRe: .NET Core & (auto)binding: Is it a bug? Pin
raddevus16-Jun-24 6:52
mvaraddevus16-Jun-24 6:52 
GeneralRe: .NET Core & (auto)binding: Is it a bug? Pin
Richard Deeming16-Jun-24 21:52
mveRichard Deeming16-Jun-24 21:52 
GeneralRe: .NET Core & (auto)binding: Is it a bug? Pin
raddevus17-Jun-24 2:10
mvaraddevus17-Jun-24 2:10 
GeneralRe: .NET Core & (auto)binding: Is it a bug? Pin
jochance18-Jun-24 3:05
jochance18-Jun-24 3:05 
GeneralUntested Backups with IonicZip Pin
kmoorevs2-Jun-24 5:30
kmoorevs2-Jun-24 5:30 
GeneralRe: Untested Backups with IonicZip Pin
MarkTJohnson4-Jun-24 5:19
professionalMarkTJohnson4-Jun-24 5:19 
GeneralRe: Untested Backups with IonicZip Pin
kmoorevs4-Jun-24 6:49
kmoorevs4-Jun-24 6:49 
GeneralLooking at the very topmost line ... Pin
trønderen1-Jun-24 11:21
trønderen1-Jun-24 11:21 
GeneralRe: Looking at the very topmost line ... Pin
Greg Utas1-Jun-24 16:42
professionalGreg Utas1-Jun-24 16:42 
GeneralRe: Looking at the very topmost line ... Pin
Daniel Pfeffer1-Jun-24 19:57
professionalDaniel Pfeffer1-Jun-24 19:57 
GeneralRe: Looking at the very topmost line ... Pin
TNCaver4-Jun-24 5:58
TNCaver4-Jun-24 5:58 
GeneralRe: Looking at the very topmost line ... Pin
trønderen4-Jun-24 14:37
trønderen4-Jun-24 14:37 
GeneralRe: Looking at the very topmost line ... Pin
Daniel Pfeffer4-Jun-24 20:19
professionalDaniel Pfeffer4-Jun-24 20:19 
GeneralRe: Looking at the very topmost line ... Pin
trønderen5-Jun-24 3:21
trønderen5-Jun-24 3:21 
GeneralRe: Looking at the very topmost line ... Pin
Daniel Pfeffer5-Jun-24 8:29
professionalDaniel Pfeffer5-Jun-24 8:29 
GeneralRe: Looking at the very topmost line ... Pin
jochance6-Jun-24 6:31
jochance6-Jun-24 6:31 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.