Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix to work with new NuGet PackageReference style in VS2017 #173

Open
bednart opened this issue Mar 29, 2018 · 10 comments · May be fixed by #200

Comments

@bednart
Copy link

commented Mar 29, 2018

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

@CZEMacLeod

This comment has been minimized.

Copy link
Contributor

commented Mar 29, 2018

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.

@ManfredLange

This comment has been minimized.

Copy link

commented May 25, 2018

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.

@cleanxfd

This comment has been minimized.

Copy link

commented Mar 19, 2019

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.

@cleanxfd

This comment has been minimized.

Copy link

commented Mar 19, 2019

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.

@davidebbo

This comment has been minimized.

Copy link
Contributor

commented Mar 19, 2019

@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.

@Jehoel

This comment has been minimized.

Copy link
Member

commented Mar 19, 2019

I'm also able to accept, merge and publish PRs too.

@declspec

This comment has been minimized.

Copy link

commented May 8, 2019

@davidebbo @Jehoel I've created a tentative PR that addresses this.

@declspec

This comment has been minimized.

Copy link

commented May 8, 2019

@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.

@davidebbo

This comment has been minimized.

Copy link
Contributor

commented May 9, 2019

@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.

@declspec

This comment has been minimized.

Copy link

commented May 10, 2019

@davidebbo No problem, the comment hack is enough for us for now (albeit not the nicest solution).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
7 participants
You can’t perform that action at this time.