Location>code7788 >text

Remember a time when Razor Pages could not be compiled and solve the problem.

Popularity:693 ℃/2024-09-30 12:14:53

The solution is written in the front: update Visual Studio and related components, my version from 17.8.0 to 17.11.4

The origin of the company's a business interface, in there is some information that needs to be displayed in the application embedded webview, quite a lot of information, involving the front-end technology is not complex, but the spelling of the string is too wordy, so the idea of adding a Razor page, so the conventional logic, registered on the service '''AddRazorPages'''', after the construction of the use of the mapping method ''' MapRazorPages''', and then debug. This time, there is no problem with debugging, and the page can be displayed normally. Because it is a small program of WeChat, and is demonstrated on the phone, the website address should be on the configured business domain, so basically after writing the page, the first time the application was deployed to the test port of iis. Then the problem occurred ---- the configured home page returned 404, and access to the url where the business is located also returned 404.

Since it was almost the first time I officially used Razor, I tried checking the url, completing the index and even completing the .cshtml, and it all came up with the same 404. Then later, I checked the application's logs, and found that there were no logs other than the w3cLog record of the page access, but the page used an Ef core query to the SQl Server, and the queried log entries weren't But the page used Ef core to query SQl Server, and the queried log entries were not outside the log filtering rules, so it was basically confirmed that the page was not executed.

Local debugging can be executed, after deployment can not be executed, the first time is to think of the environment is not consistent, Development and Production, corresponding to the development environment and production environment. In Visual Studio use debugging ASPNETCORE_ENVIRONMENT variable to be set to "Development", but the direct execution, the environment variable is not defined, the default state of the application is Production.

Running the executable in the Debug folder resulted in a still 404. noting that the Pages folder was not there, the first consideration was that there were two possibilities: one, the project was set up incorrectly and the files were not copied to the folder when it was generated; and two, consider that the cshtml file was compiled into the dll (which is what actually happened, but at the time, I wasn't able to confirm it).

Consider the first possibility first, this is not difficult to do in practice, just need to add in the project configuration

    <Content Update="Pages\*.cshtml">
      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
    </Content>

Generate again, the first time do not configure the ASPNETCORE_ENVIRONMENT variable, the result is that the relevant page is still 404, the second time, configure the ASPNETCORE_ENVIRONMENT for Development, finally, the page is displayed normally. But strangely, further release -> runtime, still need to set the ASPNETCORE_ENVIRONMENT variable to Development? This shouldn't be! (At this time I still do not think there is any problem copying Pages/*.cshtml, just think that the configuration is screwed up) set to the development environment is obviously unreasonable, a lot of configurations are for the development environment, SwaggerUI will be displayed, can not be published to the server before changing the code again, right?

However, due to the next day to demonstrate, and because of the various ** threshold of the WeChat small program, had to be deployed, and at this time IIS is in a mood, according to the online approach, and configuration, again, the configurationsThe service is always running up the production environment, we can not change the system environment variables, after all, above the old project to recognize this variable may be excited to draw the past is not certain. However, the approach is always more than difficult, the way to solve this problem, in fact, it is also the beginning of the idea of it, but in order not to move the existing code, has not been willing to use, follow the old ancestors of the principle of open and closed well. But the next day's demonstration is more important, had to make a decision against the ancestors ~!

Demo over, immediately back to this problem, the first thought is to create a new project. This is a big deal, no matter change the environment variable or copy the file, all became 404. Obviously is the default Razor Pages template, go to MSDN to give the same code, but just can not run. I searched all sorts of ways like a fly on the wall, but none of them were specific to my problem, and I searched for answers from Wenxin Yiyin and chatGPT, but they were not to my liking.

However, eventually, in various keyword searches, I found a reply from Mr. Yang Zhongke on Baike, "How to make core mvc release without compiling cshtml view", which is always relevant although it is opposite to what I think. The article describes ways to make cshtml files compile at runtime, and I felt that I could find some hints, so I gave it a try according to what he said, and the page is no longer 404.

Perhaps at this point, it is time to end, after all, with this method is not in the consideration of modifying environment variables, and do not have to modify other code, the problem is considered to be satisfactorily resolved.... Is it? Of course it can be explored no further, but this solution in the opposite direction gave me a new inspiration ----:

The article is full of references to additional configuration to be done for runtime compilation, so what if it's not done? What does it really do by default. The two property tags mentioned in Mr. Yang's article already give the answer away: MvcRazorCompileOnPublish is set to false, and RazorCompileOnBuild is also set to false, which means that, by default, the Razor files are probably compiled at generation time and at publication time. As for the form of compilation, it is possible that it is jsp like that! (As for why I knew jsp first, I was taught it in school, probably in 2020~2021, haha :), and I guess it's still being taught now. (It is also the process of completing the homework occasionally contacted the jsp file compiled decompiled code)

To explore the compiled files, naturally ILSpy is indispensable, in the past used it to analyze the problems of online code, so the thought of decompiling came to mind. After the release of the dll (not using runtime compilation configuration) to analyze, found that the relevant namespace under the compilation of only the Model file, the page content-related code is not at all.

But now, in the current environment, I am also at the end of my rope, new projects are like this, even if there is another configuration problem, to find out is not like a needle in a haystack, and so desperate, in the first day after work to try to have happened on their own computer. The only way is to use this solution ---- virtual machine. On a virtual machine with a Windows 10 environment, I rebuilt another set of Visual Studio, created a new Razor template and ran it. But this time, it worked, using all sorts of poses. I don't know whether to say it was expected or unexpected. But finally, a successful sample for me to study.

Get the virtual machine after the release of the dll, thrown into the ILSpy, this time you can clearly see the content of the page was compiled into the dll, and not surprisingly, the overall form and jsp is almost the same.

But why? This encounter showed me that cshtml does have to be compiled into the dll without special settings, and accordingly, copying cshtml to the build folder is not necessary. The configuration problem I was considering in the first place just doesn't seem to exist ......
The rest is an endless ravaging of the project. As I was generating, debugging or releasing again and again, I noticed a warning:

The CS9057 analyzer assembly "C:\Program Files\dotnet\sdk\8.0.201\Sdks\\\source-generators\" references the compiler version "4.9.0.0". ", which is higher than the currently running version "4.8.0.0".

Normally, I wouldn't care about it at all. After all, it doesn't stand out from the screen full of "property not assigned in constructor", and even if I did notice it by chance, I've already gotten acquainted with it by chance many times over. Usually there are no strange exceptions related to it. But this time, I can't ignore it because it's in the Compiler and SourceGenerators. cshtml is not generated at compile time.

During the previous ravages, I also tried using dotnet publish, and it worked, changing different sdk versions. But since noticing this warning, I've tried to find it in dotnet generation and publishing, always to no avail.
VS compiler version is not compatible anymore? How can I update it?The vs compiler uses a sdk that is not the one I have installed?

At this point, naturally thought of updating Visual Studio, find vs installer found that there is indeed a new version, found that the version number difference is not very big, so at first did not report too much hope, but after the update and then compile, all the problems are gone ......

plants sprouting

I can't believe that a problem that's been bothering me for so long was solved in one update, and there's absolutely no fantasized surge of power to save the day ah ah ah ah ah ah ah ah ah ah ah ah ah ah ah ah ah ~~~