RazorGenerator
RazorGenerator copied to clipboard
Fix to work with new NuGet PackageReference style in VS2017
Hi, we were using RazorGenerator.MsBuild in our csproj for a long time without any problem. After we switch csproj to new NuGet RestoreProjectStyle=PackageReference in VS2017 RazorGenerator failed during build.
One important note. We don't use Custom Tool at cshtml file properties (RazorGenerator) and also we don't use directives at top of cshtml files (@* Generator : MvcHelper .... *@
).
Problem is in RazorGenerator.Core.IsMvcProject method which works only for old NuGet package reference style: <Reference Include="System.Web.Mvc, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL"><HintPath>..\..\packages\Microsoft.AspNet.Mvc.5.2.3\lib\net45\System.Web.Mvc.dll</HintPath> <Private>True</Private></Reference>
.
For new one it returns False and thats the problem:
<PackageReference Include="Microsoft.AspNet.Mvc"><Version>5.2.3</Version></PackageReference>
There is easy workaround for this. We just add razorgenerator.directives
file to root directory of your cshtml files with content like this: RazorVersion: 3 Generator: MvcView
and build works again.
PS: RazorVersion: 3 is used for Microsoft.AspNet.Mvc version 5. For version 4 use RazorVersion: 2
I hadn't found the use of razorgenerator.directives, but did see that the detection for Mvc version from the project file was just hunting for the string, not actually parsing the msbuild file in any way. Thus I just added the required Reference string but commented out and it worked for me. It would be much better if it took the Reference and PackageReference collections in as part of the task and worked that way. Obviously package 5.2.4 has also now been released but I'm not sure what the detection logic there is.
The suggested workaround using razorgenerator.directives
doesn't work in our environment.
Using package Microsoft.AspNet.Mvc
version 5.2.6. Project targets full .NET Framework 4.6.2. Project file has been converted to using <PackageReference>
instead of packages.config
as recommended.
We have over 200 cshtml files and we are faced with adding @* Generator: MvcHelper *@
in each of these files as a workaround.... Also, it appears as if as a consequence RazorGenerator no longer adds the suffix _
to generated class names either leading to compile errors because of ambiguities.
Reporting errors seems to be one at a time, so with over 200 files we would have to recompile over 200 times. It would be better if all errors are reported instead of failing the build as soon as the first error is encountered.
Our project is using areas, so there are more than one root folder for the views. Or can we place razorgenerator.directives
just at the root of the project and then it will apply to all areas as well?
Update: What seems to have worked in our case is adding razorgenerator.directives
to all areas, i.e. in the Views
folder of each area as well as in the normal Views
(outside of all areas). Since for some reasons the generated files are not picked up at this stage, we then added the following to our csproj file:
<ItemGroup> <Compile Include="obj\CodeGen\**\*.cs" /> </ItemGroup>
When RazorGenerator.MsBuild
then generates the views, editor templates, display templates and partials, they will be picked up by this globbing pattern. When you add the nuget package RazorGenerator.MsBuild
this globbing pattern is not added, but it would be nice if it did.
As a result we now have a solution that allows us to use PackageReference
and RazorGenerator.MsBuild
in combination.
Wasn't sure where to post this, but in your MVC project do you have a reference to System.Web.Mvc? If not you need this and likely don't need the Generator directive. Let me know if that solves it.
Wasn't sure where to post this, but in your MVC project do you have a reference to System.Web.Mvc? If not you need this and likely don't need the Generator directive. Let me know if that solves it.
@davidebbo I'd suggest adding this to the readme as it may help others who may be consuming a library that extends from System.Web.Mvc and they may have dropped the direct reference which turns out to be required. I'd posted this on the VS developer-community site as well.
@cleanxfd I'm not actively maintaining this project anymore, but if you send a PR for the readme, we can get that in. You can directly edit the file, and that will automatically create a fork and a PR.
I'm also able to accept, merge and publish PRs too.
@davidebbo @Jehoel I've created a tentative PR that addresses this.
@bednart We also found we could also work around this by adding a simple XML comment to our .csproj:
<!-- System.Web.Mvc, Version=5 -->
Which will let RazorGenerator return the correct values (for us) from IsMvcProject
.
@declspec sorry, I've been very disconnected from this project, and from the .NET world in general since I left MS. Hoping @Jehoel or @pranavkm are able to help with the PR.
@davidebbo No problem, the comment hack is enough for us for now (albeit not the nicest solution).
There is a new PR (#213 ), could @pranavkm or @Jehoel please take a look, so a new version of Razor Generator can be created with this fix? Thanks in advance!
@monkey-ldb I've scheduled time in my calendar next week to review and test the PR and publish the updates.
Thanks for your answer. It seems that you couldn't make it.
Anyway, happy holidays.
I’m working on this today. I needed to get my VS2019 install updated. My apologies.
I now have RazorGenerator's Custom Tool working in VS2019 - phew. The VSIX is also compatible with VS2015 and VS2017.
Caveats:
- The code in RazorGenerator to load the "v1" (ASP.NET 3.5, .NET Framework 2.0-3.5), and "v2" (ASP.NET 4.0) versions of the Razor libraries are... convoluted. I think it would be prudent to simply cut-off those vestigal versions now, no?
- No support for ASP.NET Core's version of Razor. This is increasingly more important so I'll see what I can do.