在 ASP.NET Core 中使用 ApplicationPart 的简单示例
2019-09-25 11:48 音乐让我说 阅读(1470) 评论(0) 编辑 收藏 举报1. 项目截图:
2. 代码
<Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <TargetFramework>netcoreapp2.2</TargetFramework> </PropertyGroup> <ItemGroup> <Content Include="Views\MyShared\Index.cshtml"> <CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory> </Content> </ItemGroup> <ItemGroup> <EmbeddedResource Include="Views\**\*.cshtml" /> </ItemGroup> <ItemGroup> <PackageReference Include="Microsoft.AspNetCore.App" /> </ItemGroup> </Project>
请注意这里的 <EmbeddedResource Include="Views\**\*.cshtml" />
public class StartupViews { public StartupViews(IConfiguration configuration) { Configuration = configuration; } public IConfiguration Configuration { get; } #region snippet public void ConfigureServices(IServiceCollection services) { services.Configure<RazorViewEngineOptions>(options => { options.FileProviders.Add( new EmbeddedFileProvider(typeof(MySharedController).GetTypeInfo().Assembly)); }); // Requires using System.Reflection; var assembly = typeof(MySharedController).GetTypeInfo().Assembly; services.AddMvc() .AddApplicationPart(assembly) .SetCompatibilityVersion(CompatibilityVersion.Version_2_2); } #endregion public void Configure(IApplicationBuilder app, IHostingEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } else { app.UseExceptionHandler("/Home/Error"); app.UseHsts(); } app.UseHttpsRedirection(); app.UseStaticFiles(); app.UseCookiePolicy(); app.UseMvc(routes => { routes.MapRoute( name: "default", template: "{controller=Home}/{action=Index}/{id?}"); }); } }
谢谢浏览!
作者:音乐让我说(音乐让我说 - 博客园)
出处:http://music.cnblogs.com/
文章版权归本人所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。