Orchard Core Framework:ASP.NET Core 模块化,多租户框架

Orchard Core Framework:ASP.NET Core 模块化,多租户框架html

上一篇编写Orchard Core一分钟搭建ASP.NET Core CMS ,介绍ASP.NET Core CMS ,Orchard的ASP.NET Core版,同时对应有一个ASP.NET Core框架。git

支持模块化和多租户。整个Orchard Core就是经过一个个模块Module组成的github

首先建立一个空的 ASP.NET Core Web应用程序为基础。下面学习模块的创建及使用。web

模块化

首先在以前建立好的ASP.NET Core Web应用程序中,新建一个 类库(.NET Core)项目 为ModuleWeb。shell

而后添加 Microsoft.AspNetCore.Mvc 及 OrchardCore.Module.Targets 引用。json

命令以下:app

Install-Package Microsoft.AspNetCore.Mvc框架

Install-Package OrchardCore.Module.Targets -Preide

 

 接着咱们就能够添加一个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 

相关文章
相关标签/搜索