Skip to content
This repository was archived by the owner on Nov 29, 2018. It is now read-only.
This repository was archived by the owner on Nov 29, 2018. It is now read-only.

Improve support for precompilation of razor views in class libraries (embedded ui) #214

@evil-shrike

Description

@evil-shrike

As continue of #187

The problem: I need a class library with embedded UI.
Currently it's hard implement such setup when a class library in a solution contains pre-compilable razor views. Lloading views from embedded resource is another option and it works fine.

At the end of the mentioned discussion (#187) there was posted a link to blog post about how to implement embedded UI - https://dzone.com/articles/self-contained-ui-running-one-aspnet-core-mvc-site
It seems to work well. But looks hacky, too much cleaver things should be done in csproj. Another problem it's not documented in official docs.

So this issue is an suggestion to have more simplified approach for "embedded UI", i.e. keeping razor views in class libraries and automatically compile and deploy them.

Some nuances which should be elaborated:

  • a solution with app and lib both can contain views and identical routes (like "Home/Index")
  • solution should start with F5/CtrlF5 in VS (views should be compiled and deploy)
  • on deployment assemblies with pre-compiled views should be deployed correctly
  • ideally there shouldn't be required to make a library project "runnable" (set Sdk="Microsoft.NET.Sdk.Web" and declare static Main method) but it's not critical indeed, at least it should be documented

Currently (aspnetcore2.0) we have to do the following (thanks to @dasMulli for describing it in this comment - #71 (comment))

		<MvcRazorCompileOnPublish>true</MvcRazorCompileOnPublish>
	<Target Name="SetMvcRazorOutputPath">
		<PropertyGroup>
			<MvcRazorOutputPath>$(OutputPath)</MvcRazorOutputPath>
		</PropertyGroup>
	</Target>
	<Target Name="_MvcRazorPrecompileOnBuild" DependsOnTargets="SetMvcRazorOutputPath;MvcRazorPrecompile" AfterTargets="Build" Condition=" '$(IsCrossTargetingBuild)' != 'true' " />
	<Target Name="IncludePrecompiledViewsInPublishOutput" DependsOnTargets="_MvcRazorPrecompileOnBuild" BeforeTargets="PrepareForPublish" Condition=" '$(IsCrossTargetingBuild)' != 'true' ">
		<ItemGroup>
			<_PrecompiledViewsOutput Include="$(MvcRazorOutputPath)$(MSBuildProjectName).PrecompiledViews.dll" />
			<_PrecompiledViewsOutput Include="$(MvcRazorOutputPath)$(MSBuildProjectName).PrecompiledViews.pdb" />
			<ContentWithTargetPath Include="@(_PrecompiledViewsOutput->'%(FullPath)')" RelativePath="%(_PrecompiledViewsOutput.Identity)" TargetPath="%(_PrecompiledViewsOutput.Filename)%(_PrecompiledViewsOutput.Extension)" CopyToPublishDirectory="PreserveNewest" />
		</ItemGroup>
	</Target>

Off the top of my head I'd suggest to introduce MvcRazorCompile=true.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions