Aspx and Ascx Files Broken by External Source Directives Visual Studio
Here’s a fun one that I’ve had ever since Visual Studio 2015 through to Visual Studio 2019. Occasionally when editing an aspx or ascx my entire file is updated by the IDE to include #EXTERNAL SOURCE directives throughout my file and basically corrupts the entire thing.
This would appear to happen randomly whenever I clicked into an ASP tag <% %>, edited If statements within ASP tags or even when clicking around or out of the file.
I even had a couple of co-workers eventually run into the same problem (they didn’t deal as much with web as me so hadn’t seen it at first) and none of us could figure out what was going on. We just had to be extra careful to make sure we didn’t save and close the file with any of these directives included. Depending on the size of the file these could appear anywhere out of view and you wouldn’t know.
Turns out this is due to Visual Studio automatically trying to pretty-format the code in these file types!
Here is the answer given by the Visual Studio team when I opened a ticket which was subsequently closed as being too low of a priority.
Also, could you try turning off the “Pretty listing (reformatting) of code” option in Tools->Options->Text Editor->Basic->Advanced, and see if that has any effect? That could help us narrow down the source of the problem.
https://developercommunity.visualstudio.com/content/problem/211087/external-source-directives-added-to-ascx-files-aut.html
After years of frustration and one day finally being able to consistently replicate the issue – I no longer have to deal with it!
Hopefully this information helps someone else.