[英]Copy Views folder for Asp.Net in .Net5
Many things changes from version to version in Asp.Net depending on .Net version.根据 .Net 版本的不同,Asp.Net 中的许多内容会因版本而异。 I have a project for .Net5 and I cannot get Views folder added in my build folder.我有一个 .Net5 项目,但无法在我的构建文件夹中添加 Views 文件夹。 I've tried to add next lines to my csproj file:我试图将下一行添加到我的 csproj 文件中:
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<PreserveCompilationContext>true</PreserveCompilationContext>
<RazorCompileOnBuild>false</RazorCompileOnBuild>
</PropertyGroup>
The result is that I still don't have Views foler at debug, release or publish folder.结果是我在调试、发布或发布文件夹中仍然没有视图文件夹。 If I try to run in debug mode I get an error of missing views:如果我尝试在调试模式下运行,我会收到缺少视图的错误:
InvalidOperationException: The view 'Index' was not found. The following locations were searched:
/Views/Home/Index.cshtml
/Views/Shared/Index.cshtml
This is my ConfigureServices method:这是我的 ConfigureServices 方法:
public void ConfigureServices(IServiceCollection services)
{
services.AddHttpContextAccessor();
services.AddAuthentication(IdentityConstants.ApplicationScheme)
.AddCookie(IdentityConstants.ApplicationScheme, o =>
{
o.Cookie.Path = "/";
});
services.AddControllersWithViews(o => o.Filters.Add(new AuthorizeFilter()));
}
Adding CopyRazorGenerateFilesToPublishDirectory
to csproj changes nothing.将CopyRazorGenerateFilesToPublishDirectory
添加到 csproj 没有任何改变。
<CopyRazorGenerateFilesToPublishDirectory>true</CopyRazorGenerateFilesToPublishDirectory>
There are some solutions for this on stackoverflow, but all about older versions like .net framework and .net core 2 or 3. None of them has worked for me.在 stackoverflow 上有一些解决方案,但都是关于旧版本的,如 .net 框架和 .net core 2 或 3。它们都没有为我工作。
EDIT: Adding a reference to Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation package and setting Razor RuntimeCompilation at services configuration ...编辑:添加对 Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation 包的引用并在服务配置中设置 Razor RuntimeCompilation ...
services.AddControllersWithViews()
.AddRazorRuntimeCompilation();
...you can edit your views while running your app from VS without recompiling (not checked). ...您可以在从 VS 运行您的应用程序时编辑您的视图,而无需重新编译(未选中)。 Then if I add CopyRazorGenerateFilesToPublishDirectory
in my csproj I have editable views in my publish folder.然后,如果我在我的 csproj 中添加CopyRazorGenerateFilesToPublishDirectory
,我的发布文件夹中有可编辑的视图。 The problem is that when I modify any view I get an exception: System.InvalidOperationException: Cannot find reference assembly 'Microsoft.AspNetCore.Antiforgery.dll' file for package Microsoft.AspNetCore.Antiforgery.Reference
.问题是,当我修改任何视图时,我得到一个异常: System.InvalidOperationException: Cannot find reference assembly 'Microsoft.AspNetCore.Antiforgery.dll' file for package Microsoft.AspNetCore.Antiforgery.Reference
。 No idea how to solve it.不知道如何解决它。
Maybe try to inspect newly created empty project也许尝试检查新创建的空项目
dotnet new webapp --razor-runtime-compilation
it will create the following project它将创建以下项目
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<CopyRefAssembliesToPublishDirectory>false</CopyRefAssembliesToPublishDirectory>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation" Version="5.0.0" />
</ItemGroup>
</Project>
Now check the Startup.cs file for ConfigureServices
method:现在检查Startup.cs文件中的ConfigureServices
方法:
public void ConfigureServices(IServiceCollection services)
services.AddRazorPages();
// services.AddMvc();
// services.AddRazorPages().AddRazorRuntimeCompilation();
}
Here we see services.AddRazorPages() extention method being called, so we could expect Razor Views functionality while calling services.AddControllersWithViews()
在这里,我们看到 services.AddRazorPages() 扩展方法被调用,因此我们可以在调用services.AddControllersWithViews()
时期望 Razor Views 功能
To summarize, adding nuget package reference Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation
and calling services.AddControllersWithViews
or services.AddMvc
should add support for Razor views into the project.总而言之,添加 nuget 包引用Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation
并调用services.AddControllersWithViews
或services.AddMvc
应该会将对 Razor 视图的支持添加到项目中。
Also configuring your views location is like calling:同样配置您的视图位置就像调用:
services.AddControllersWithViews()
.AddRazorOptions(options =>
{
options.ViewLocationFormats.Add("/{0}.cshtml");
});
Checking the sources: https://source.dot.net/#Microsoft.AspNetCore.Mvc/MvcServiceCollectionExtensions.cs,27 :检查来源: https : //source.dot.net/#Microsoft.AspNetCore.Mvc/MvcServiceCollectionExtensions.cs,27 :
public static IMvcBuilder AddMvc(this IServiceCollection services)
{
if (services == null)
{
throw new ArgumentNullException(nameof(services));
}
services.AddControllersWithViews();
return services.AddRazorPages();
}
The problem is that when I modify any view I get an exception: System.InvalidOperationException: Cannot find reference assembly 'Microsoft.AspNetCore.Antiforgery.dll' file for package Microsoft.AspNetCore.Antiforgery.Reference.问题是,当我修改任何视图时,我得到一个异常:System.InvalidOperationException:找不到包 Microsoft.AspNetCore.Antiforgery.Reference 的参考程序集“Microsoft.AspNetCore.Antiforgery.dll”文件。 No idea how to solve it.不知道如何解决它。
Having <CopyRefAssembliesToPublishDirectory>false</CopyRefAssembliesToPublishDirectory>
the issue of missing assembly 'Microsoft.AspNetCore.Antiforgery.dll'
reference was reproduced after running application from the published ( dotnet publish
) folder, and trying making changes the Views.具有<CopyRefAssembliesToPublishDirectory>false</CopyRefAssembliesToPublishDirectory>
缺少程序集'Microsoft.AspNetCore.Antiforgery.dll'
引用的问题在从已发布 ( dotnet publish
) 文件夹运行应用程序并尝试更改视图后重现。
After reverting <CopyRefAssembliesToPublishDirectory>true</CopyRefAssembliesToPublishDirectory>
the issue had gone.恢复<CopyRefAssembliesToPublishDirectory>true</CopyRefAssembliesToPublishDirectory>
,问题就消失了。
Here is my .csproj
that should work properly with dotnet publish
这是我的.csproj
应该与dotnet publish
一起正常工作
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<RazorCompileOnBuild>false</RazorCompileOnBuild>
<CopyRefAssembliesToPublishDirectory>true</CopyRefAssembliesToPublishDirectory>
<CopyRazorGenerateFilesToPublishDirectory>true</CopyRazorGenerateFilesToPublishDirectory>
<PreserveCompilationReferences>true</PreserveCompilationReferences>
<PreserveCompilationContext>true</PreserveCompilationContext>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation" Version="5.0.5" />
</ItemGroup>
<ItemGroup>
<KnownFrameworkReference Update="Microsoft.AspNetCore.App">
<TargetingPackVersion>5.0.0</TargetingPackVersion>
</KnownFrameworkReference>
</ItemGroup>
</Project>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.