RazorGenerator icon indicating copy to clipboard operation
RazorGenerator copied to clipboard

Views do not recompile if namespaces section was changed in web.config from views forlder.

Open itd3v opened this issue 6 years ago • 5 comments

So as the exmaple of an issue I created new project and replaced some extensions and objects from web project to new one. Then I've added new namespace to system.web.webPages.razor -> pages -> namespaces section of web.config located in 'Views' folder and got the errors while building project. The reason is missing namespace in views.

On developer machine I can just clear folder with compiled views but what I need to do on production server to force views recompilation, I have no access to production server.

I really like RazorGenerator, it's pretty cool feature!!! Thanks for attention.

itd3v avatar Oct 25 '19 14:10 itd3v

Views do not recompile if namespaces section was changed in web.config from views forlder

RazorGenerator currently does not check the web.config last-modified-times or compare file hashes to trigger recompilation - because the main use-case of RazorGenerator is when you don't want to publish the cshtml files.

On developer machine I can just clear folder with compiled views but what I need to do on production server to force views recompilation, I have no access to production server.

When you're developing, ASP.NET will use its own BuildManager with its own Razor view-engine which does check web.config files, and won't invoke RazorGenerator - when you publish the project will defer to RazorGenerator always.

It sounds like you're asking how precompile your views while also allowing them to be updatable on the server?

daiplusplus avatar Oct 25 '19 20:10 daiplusplus

Hi Jehoel! Thanks for your reply.

It sounds like you're asking how precompile your views while also allowing them to be updatable on the server?

I need a way to force rebuild views by RazorGenerator for Release configuration. If there is no such setting, I think I need to update deployment script to delete CodGen folder before building a web project.

itd3v avatar Oct 26 '19 05:10 itd3v

I need a way to force rebuild views by RazorGenerator for Release configuration.

They should rebuild any time you do a project Rebuild - there's nothing special in the RazorGenerator MSBuild targets.

What does your build process look like?

daiplusplus avatar Oct 28 '19 17:10 daiplusplus

Hello there! May be a test case will be helpful to understand an issue...

  • Visual Studio 2017
  • .NET Framework 4.6.1
  • C#
  1. Create ASP.NET MVC 5 project (namespace Web) with RazorGenerator.Mvc and RazorGenerator.MsBuild dependencies. By default namespace Web will be added to ~/Views/Web.config file system.web.webPages.razor -> pages -> namespaces section as <add namespace="Web" /> child.
  2. Create a class NumberExt (namespace Web) in the root of web app project. Add there some const or static field int PercentPrecision.
  3. Use NumberExt.PercentPrecision somewhere in _Layout.cshtml and run the application.
  4. Change NumberExt namespace from Web to Web.Common.
  5. Append Web.Common namespace to ~/Views/Web.config file system.web.webPages.razor -> pages -> namespaces section as <add namespace="Web.Common" /> child.
  6. Restart solution to allow Visual Studio update shared namespaces in views syntax, otherwise it will cause an error 'Missing Reference'.
  7. Try to build the project.

Result: You will have an error "The name 'NumberExt' does not exist in the current context" for compiled _Layout.cshtml.cs because using namespaces were not updated inside the compiled file. Expected Result: Updated using namespaces in compiled views.

Thanks for attention and have a good day!

itd3v avatar Oct 29 '19 09:10 itd3v

Ok, I've fixed it by declearing RazorViewsCodeGenDirectory in web app .csproj file before importing RazorGenerator.MsBuild.targets:

<PropertyGroup Label="RazorGenerator">
  <RazorViewsCodeGenDirectory Condition=" '$(RazorViewsCodeGenDirectory)' == '' ">$(MsBuildProjectDirectory)\obj\CodeGen\</RazorViewsCodeGenDirectory>
</PropertyGroup>

Appended task RemoveDir to target BeforeBuild like these:

<Target Name="BeforeBuild">
  _YourBeforeBuildActionIfAny_
  <RemoveDir Directories="$(RazorViewsCodeGenDirectory)" Condition="Exists('$(RazorViewsCodeGenDirectory)')" ContinueOnError="true" />
</Target>

itd3v avatar Nov 13 '19 09:11 itd3v