Why didn't Visual Studio tell me the error was in the view?
I did something a bit silly - I created a new project and called it "Directory", not realising and forgetting that this was a language/reserved word.
Anyway, I had my design brief and just got on with work.
After a good few hours, I was getting ready to build for the first time, and it built fine. I then went to run and it didn't work.
(At this point, I turned on View Compiling)
After trying to understand the error, I realised why it wasn't working and I went to rename in places.
I changed the project and solution name from Directory
to WebDirectory
, and I changed all references where I could see it.
But, the project just wouldn't compile and it gave me these errors:
After going in to this file and trying to understand it a bit better, I could see that I missed out renaming a few views. The moment I open these views, I get the following errors:
Now, if I had those errors straight away, it would have made it so much easier for me to see what/where the error is.
I have Compile Views turned on and I am guessing that the first image is the error showing up in the compiled view, but, why doesn't actually show the error in the view, which it is more than capable of doing (see second picture), which would be a lot more helpful/useful than the first one as I can't even change them!?
(Bonus points / not worth a separate question if someone can tell me if开发者_如何学Go it is safe to rename the folders that the project is in, now that I have renamed the project/solution from within VS)
It is probably due to the fact that the Razor engine, while very well integrated into VS, is not an intrinsic part of the IDE. Remember that MVC is an add-on library.
The CS files that you saw the first screenshot are probably generated CS from "Compiled Views" being turned on. Until you turned on Compiled Views, those files simply didn't exist. Turning on compiled views created them and gave you a compile error (.cshtml files are not compiled until you visit the page, and thus do not qualify as compile errors).
The second screenshot comes from opening the cshtml files, which allows Razor a chance to compile evaluate them and show 'pseudo-compile-time' errors. Having razor attempt to compile all of the .cshtml files in the project at compile time (as opposed to page-view time) probably is very difficult at best and semantically impossible at worst.
精彩评论