创建控制台项目
使用类库项目,.net standard.2.0
nuget安装RazorGenerator.MsBuild
这样在项目的packages目录能看到RazorGenerator.MsBuild.2.5.0
razor页类定义
namespace RazorLib
{
public abstract class RazorPage
{
public virtual void Execute()
{
}
public virtual void WriteLiteral(string str)
{
}
}
}
index.cshtml
@* Generator: Template TypeVisibility: Internal GeneratePrettyNames: True TrimLeadingUnderscores : true *@
@inherits RazorLib.RazorPage
<div class="text-center">
<h1 class="display-4">Welcome</h1>
<p>Learn about <a href="https://learn.microsoft.com/aspnet/core">building Web apps with ASP.NET Core</a>.</p>
</div>
开头的注释行是必须的,具体含义到RazorGenerator.MsBuild的github查看
添加Razor.build到项目
需要将RootNamespace改为你自己的
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="PrecompileRazorFiles" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="..\packages\RazorGenerator.MsBuild.2.5.0\build\RazorGenerator.MsBuild.targets" />
<PropertyGroup>
<RootNamespace>RazorLib</RootNamespace>
<GeneratePrettyNames>true</GeneratePrettyNames>
<RazorViewsCodeGenDirectory>$(MsBuildProjectDirectory)\</RazorViewsCodeGenDirectory>
</PropertyGroup>
</Project>
编辑项目文件RazorConsole.csproj
追加以下内容:
<PropertyGroup>
<TargetFrameworks>netstandard2.0;</TargetFrameworks>
<DebugType>portable</DebugType>
<RootNamespace>RazorLib</RootNamespace>
</PropertyGroup>
<PropertyGroup>
<MSBuild14FullPath>$(MSBuildProgramFiles32)\..\Windows\Microsoft.NET\Framework64\v4.0.30319\MSBuild.exe</MSBuild14FullPath>
<MSBuildCurrentFullPath>$(MSBuildBinPath)\MSBuild.exe</MSBuildCurrentFullPath>
<RazorProjectFile>Razor.build</RazorProjectFile>
<InnerTargets>GenerateRazorClasses;Build</InnerTargets>
</PropertyGroup>
<Target Name="GenerateRazorClasses">
<Exec Command=""$(MSBuildCurrentFullPath)" $(RazorProjectFile) /v:quiet /nologo" Condition="Exists('$(MSBuildCurrentFullPath)')" />
<Exec Command=""$(MSBuild14FullPath)" $(RazorProjectFile) /v:quiet /nologo" Condition="Exists('$(MSBuild14FullPath)') and !Exists('$(MSBuildCurrentFullPath)')" />
<Warning Text="Classes for Razor files (*.cshtml) weren't re-generated: couldn't find the '$(MSBuild14FullPath)' or '$(MSBuildCurrentFullPath)' file" Condition="!Exists('$(MSBuild14FullPath)') and !Exists('$(MSBuildCurrentFullPath)')" />
</Target>
标签:razor,MsBuild,自定义,RazorLib,RazorGenerator,netstandard2.0,public From: https://www.cnblogs.com/readafterme/p/18499963注意要用
netstandard2.0; 不要写成netstandard2.0