AplicationPart使用
创建Razor页面类库
-
创建Razor类库,命名为: DemoPlugin2
-
生成controller和razor页面
Plugin2Controller
public class Plugin2Controller : Controller
{
public IActionResult HelloWorld()
{
return View();
}
}
HelloWorld.cshtml
Hello World!
- 将HelloWorld.cshtml设置为嵌入资源
使用
-
创建模型-视图-控制器项目
-
引入Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation, 修改Program.cs
var assembly = Assembly.LoadFile(AppDomain.CurrentDomain.BaseDirectory + "DemoPlugin2.dll"); // Add services to the container. builder.Services.AddControllersWithViews() .AddApplicationPart(assembly) .AddRazorRuntimeCompilation(); builder.Services.Configure<MvcRazorRuntimeCompilationOptions>(options => { options.FileProviders.Add(new EmbeddedFileProvider(assembly)); });
-
将Razor类库项目编译的dll放入项目的debug目录,然后运行.