且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

从 ASP.Net Core 2 API 中删除 PrecompiledViews.dll

更新时间:2023-02-15 10:12:32

你说得对,预编译步骤总是发出一个程序集并且不检查是否真的有视图.您可以通过将其放入您的 csproj 文件来禁用预编译步骤:

<MvcRazorCompileOnPublish>false</MvcRazorCompileOnPublish></PropertyGroup>

这将激活正常的编译上下文保存(refs 子文件夹).要停用此功能,请添加

false</PreserveCompilationContext>

到属性组.

In .NET Core 2 Web API app, Publish to folder feature in MS VS 2017 produce:

<ProjectAssembly>.PrecompiledViews.dll
<ProjectAssembly>.PrecompiledViews.pdb

Offical docs says that PrecompiledViews related to precompiling Razor Views, but my API doesn't contain any views or static files, just REST endpoints that return json.

Using .Net reflector I found the PrecompiledViews.dll empty.

So I deleted PrecompiledViews.dll and tested my API and it seems to work fine without any warnings or exceptions.

Is it safe to delete PrecompiledViews.dll and pdp if the API not using any razor views? If yes, Is there option in VS 2017 to stop publishing unused PrecompiledViews?

You are right, the precompile step always emits an assembly and doesn't check if there are actually views. You can disable the precompilation step by putting this into your csproj file:

<PropertyGroup>
  <MvcRazorCompileOnPublish>false</MvcRazorCompileOnPublish>
</PropertyGroup>

This will then activate the normal copilation context preservation (refs subfolder). To deactivate this as well, add

<PreserveCompilationContext>false</PreserveCompilationContext>

to the property group.