Orchard Core Framework:ASP.NET Core 模块化,多租户框架
上一篇編寫Orchard Core一分鐘搭建ASP.NET Core CMS?,介紹ASP.NET Core CMS?,Orchard的ASP.NET Core版,同時對應有一個ASP.NET Core框架。
支持模塊化和多租戶。整個Orchard Core就是通過一個個模塊Module組成的
首先創建一個空的?ASP.NET Core Web應用程序為基礎。下面學習模塊的建立及使用。
模塊化
首先在之前創建好的ASP.NET Core Web應用程序中,新建一個 類庫(.NET Core)項目 為ModuleWeb。
然后添加 Microsoft.AspNetCore.Mvc 及 OrchardCore.Module.Targets 引用。
命令如下:
Install-Package?Microsoft.AspNetCore.Mvc
Install-Package OrchardCore.Module.Targets -Pre
?
?接著我們就可以添加一個Views 文件夾和 Controllers 文件夾,以及添加一個HomeController和對應的視圖頁。
由于類庫上沒有很好的新建快捷方式,建議從ASP.NET Core Web 項目中復制。
public class HomeController : Controller
? ? {
? ? ? ? public IActionResult Index()
? ? ? ? {
? ? ? ? ? ? return View();
? ? ? ? }
? ? }
Home/Index.cshtml
<h1>Hello from ModuleWeb /Home/Index</h1> <h2>LineZero</h2>Module 創建好了,接下來在ASP.NET Core Web 項目中引用。
首先需要在Web 項目添加一個OrchardCore.Application.Mvc.Targets 包
Install-Package?OrchardCore.Application.Mvc.Targets -Pre
接著將ModuleWeb 項目引用進來。
更改Startup.cs 如下:
public class Startup
? ? {
? ? ? ? public void ConfigureServices(IServiceCollection services)
? ? ? ? {
? ? ? ? ? ? services.AddModules();
? ? ? ? }
? ? ? ? public void Configure(IApplicationBuilder app, IHostingEnvironment env)
? ? ? ? {
? ? ? ? ? ? if (env.IsDevelopment())
? ? ? ? ? ? {
? ? ? ? ? ? ? ? app.UseDeveloperExceptionPage();
? ? ? ? ? ? }
? ? ? ? ? ? app.UseModules();
? ? ? ? }
? ? }
注意項目中引用為?Microsoft.AspNetCore 以及Microsoft.ApplicationInsights.AspNetCore,配置如下
<ItemGroup>
? ? <PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.0.0" />
? ? <PackageReference Include="Microsoft.AspNetCore" Version="2.0.0" />
? ? <PackageReference Include="OrchardCore.Application.Mvc.Targets" Version="1.0.0-beta1-3667" />
? </ItemGroup>
? <ItemGroup>
? ? <ProjectReference Include="..\ModuleWeb\ModuleWeb.csproj" />
? </ItemGroup>
接著運行程序,輸入 ModuleWeb/Home/index 如下
ModuleWeb 也就是正常可用。
多租戶
多租戶,可以直接根據配置讀取用戶設置,實現多域名或者多目錄。
先來添加一個ModuleInfo ,添加引用:
Install-Package OrchardCore.Module.Targets -Pre
Install-Package OrchardCore.Environment.Shell.Abstractions -Pre
接著添加一個Startup.cs,實現如下:
public class Startup : StartupBase
? ? {
? ? ? ? // This method gets called by the runtime. Use this method to add services to the container.
? ? ? ? // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
? ? ? ? public override void ConfigureServices(IServiceCollection services)
? ? ? ? {
? ? ? ? }
? ? ? ? // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
? ? ? ? public override void Configure(IApplicationBuilder app, IRouteBuilder routes, IServiceProvider serviceProvider)
? ? ? ? {
? ? ? ? ? ? app.Map("/hello", branch =>?
? ? ? ? ? ? ? ? branch.Run(context => context.Response.WriteAsync("Hello World From ModuleInfo LineZero"))
? ? ? ? ? ? );
? ? ? ? ? ? app.Map("/info", branch =>
? ? ? ? ? ? {
? ? ? ? ? ? ? ? branch.Run(context =>
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? var shellSettings = context.RequestServices.GetRequiredService<ShellSettings>();
? ? ? ? ? ? ? ? ? ? return context.Response.WriteAsync($"Request from tenant: {shellSettings.Name}");
? ? ? ? ? ? ? ? });
? ? ? ? ? ? });
? ? ? ? }
? ? }
?訪問/info 會讀取shellsetting 獲取用戶的配置。
在ASP.NET Core Web應用程序 中添加一個tenants.json 如下:
{
? "Web": {
? ? "State": "Running",
? ? // "RequestUrlHost": "web.com",
? ? "RequestUrlPrefix": "web",
? ? "Features": [ "ModuleWeb", "ModuleInfo", "OrchardCore.Mvc" ],
? ? "MyConnectionString": "connectionstring1"
? },
? "Info": {
? ? "State": "Running",
? ? // "RequestUrlHost": "info.com, info.org",
? ? "RequestUrlPrefix": "info",
? ? "Features": [ "ModuleInfo", "OrchardCore.Mvc" ],
? ? "MyConnectionString": "connectionstring2"
? }
}
并更改Startup.cs
public void ConfigureServices(IServiceCollection services){services.AddModules(c=>c.WithTenants());}接著將ModuleInfo 添加到Web應用程序,運行應用程序。
訪問/web/info ,如下會輸出Web
訪問/info/info ,如下會輸出Info
然后Web 配置下才會有兩個模塊,Info 配置下只有一個模塊。可以根據這些信息來做用戶隔離和區分。
對于Orchard Core Framework 更深入的了解,可以查看GitHub 上的源碼:https://github.com/OrchardCMS/OrchardCore?
原文地址:https://www.cnblogs.com/linezero/p/8093234.html
.NET社區新聞,深度好文,歡迎訪問公眾號文章匯總 http://www.csharpkit.com
總結
以上是生活随笔為你收集整理的Orchard Core Framework:ASP.NET Core 模块化,多租户框架的全部內容,希望文章能夠幫你解決所遇到的問題。