It turns out that this issue was happening due to a missing SSL binding for that port as I am trying to use "https".
Running netsh command to show the current bindings did not show anything for the port 20001
netsh http show sslcert
To fix this we need to do the following:
Running the following command in a command prompt (with administrative privileges) creates a new Self signed Cert (if it does not exist already) and binds the localhost port 20001 to that cert.
> cd "C:\Program Files (x86)\IIS Express"
> IisExpressAdminCmd.exe setupsslUrl -url:https://localhost:20001/ -UseSelfSigned
The IIS Express folder is generally located inside the program files (x86) folder even in windows 11. Inside the IIS Express folder, we have many utilities such as the IisExpressAdminCmd that accepts a param called "setupsslUrl", which further requires an url param and a cert param. In the above case, for the url, I have specified https://localhost:2001 and the cert is a self-signed cert.
In my case, I already had the IIS Express Cert in the "Trusted Root Certificates" section in the windows certificate manager (certmgr). So running the command created a new binding with that cert to port 20001.
I confirmed it by running a netsh command in a command prompt (with administrative privileges).
netsh http show sslcert
the output showed:
Then I opened the certificate manager (certmgr.msc or msc.exe)
After that I ran the C# project from Visual Studio 2022 and it did not give me an error and the web site loaded correctly.