Newm
Newm

Reputation: 1403

OrchardCore Multi Tennant with Web Api

Is it possible to use the multi tennant features of OrchardCore in a Web API project?

I have created an empty project in Visual Studio using the using the "ASP.NET Core Web Application" -> "API" template and added a reference to OrchardCore.Application.Mvc.Targets.

My StartUp class looks like this:

public class Startup
  {
    public Startup(IConfiguration configuration)
    {
      Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
      services.AddControllers();
      services.AddOrchardCore().AddMvc().WithTenants();
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
      if (env.IsDevelopment())
      {
        app.UseDeveloperExceptionPage();
      }

      app.UseOrchardCore(builder =>
      {
        builder.UseHttpsRedirection();
        builder.UseRouting();
        builder.UseAuthorization();
        builder.UseEndpoints(endpoints => { endpoints.MapControllers(); });
      });
    }
}

I have also added the following to appsetting.json:

"OrchardCore": {
"Default": {
  "State": "Running",
  "RequestUrlHost": null,
  "RequestUrlPrefix": null,
  "Features": [],
  "CustomTitle": "Default Tenant",
  "CustomSetting": "Custom setting for Default tenant"
},
"CustomerA": {
  "State": "Running",
  "RequestUrlHost": null,
  "RequestUrlPrefix": "customer-a",
  "Features": [ "Module1" ],
  "CustomTitle": "Customer A",
  "CustomSetting": "Custom setting for Customer A"
},
"CustomerB": {
  "State": "Running",
  "RequestUrlHost": null,
  "RequestUrlPrefix": "customer-b",
  "Features": [ "Module1", "Module2" ],
  "CustomTitle": "Customer B",
  "CustomSetting": "Custom setting for Customer B"
}

When I run the application the default https://localhost:44370/weatherforecast route works but https://localhost:44370/customer-b/weatherforecast returns a 404.

I think it may be the way I am using AddControllers or UseOrchardCore. For example if I comment out AddControllers I get an InvalidOperationException becuase UseAuthorization no longer works.

Upvotes: 2

Views: 944

Answers (1)

Bootproject
Bootproject

Reputation: 94

Using Core 3.1

I did this in my StartUp to let me use controllers instead of pages. Keep in mind that you have to add OrchardCore.Module.Targets to each module and then import the module to the main project.

public void ConfigureServices(IServiceCollection services)
    {

        services.Configure<RazorViewEngineOptions>(options =>
        {
            options.ViewLocationExpanders.Add(new TenantViewLocationExpander());
        });


        /*
         * Needed for authorization and such.
         */
        services.AddControllersWithViews();

        /*
         * Add multitenancy
         */
        services.AddOrchardCore()
            .Configure((tenantAppBuilder, endpointBuilder, tenantScopeServiceProvier) =>
            {
                /*
                 * Map our routes
                 */
                endpointBuilder.MapControllerRoute(
                        name: "default",
                        pattern: "{controller=Home}/{action=Index}/{id?}");

            }).AddMvc()
                .WithTenants();
    }

Upvotes: 0

Related Questions