以前,在 ASP.Net Core 2.2 中,我的 Startup 类中有以下内容:
public void ConfigureServices(IServiceCollection services) {
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
}
public void Configure(IApplicationBuilder app) {
app.UseMvc();
}
任何引用的 Razor 类库都会自动显示其控制器在正确的路径上(通过控制器中的属性路由),从而呈现其正确的视图。
现在,我已将所有内容升级到 ASP.Net Core 3.0,我的 Startup 现在看起来像这样:
public void ConfigureServices(IServiceCollection services) {
services.AddControllersWithViews()
.AddRazorRuntimeCompilation()
.AddApplicationPart(typeof(MyRazorClassLibrary).Assembly)
}
public void Configure(IApplicationBuilder app) {
app.UseRouting();
app.UseEndpoints(endpoints => {
endpoints.MapControllers();
});
}
该Configure()
方法是根据规范,但ConfigureServices()
突然有很多额外的“行李”。一切似乎都有效,但手动添加 Razor 类库程序集似乎很麻烦(我有几个,它使代码的可维护性大大降低)。以前所有视图也会自动找到,但现在我必须使用Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation
NuGet 包才能让应用程序能够在 RCL 中找到视图。
在 ASP.Net Core 3.0 中是否有更简单的方法来使用带有自己的视图和控制器的 Razor 类库?