您的位置:首页 > 编程语言 > ASP

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

2017-12-27 00:00 871 查看
上一篇编写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 

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: