OrchardCMS / OrchardCore

Orchard Core is an open-source modular and multi-tenant application framework built with ASP.NET Core, and a content management system (CMS) built on top of that framework.
https://orchardcore.net
BSD 3-Clause "New" or "Revised" License
7.43k stars 2.4k forks source link

Can't call controllers inside shared project #11316

Closed atorres16 closed 5 months ago

atorres16 commented 2 years ago

I created these web projects

Module references Reusable API, and it works fine on it's own

Added Module into CMS and enabled it

The problem is, it can't find the controllers on Reusable API from the CMS


Details:

image

CMS Startup.cs

  public class Startup
    {
        // 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 void ConfigureServices(IServiceCollection services)
        {
            services.AddOrchardCms();
        }

        // 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.UseRouting();
            app.UseOrchardCore();

        }
    }

Module 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)
        {
            services.AddControllers();
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public override void Configure(IApplicationBuilder builder, IEndpointRouteBuilder routes, IServiceProvider serviceProvider)
        {
            routes.MapControllers();
       }
}

Reusable API Startup.cs

   public class Startup
    {
        // 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 void ConfigureServices(IServiceCollection services)
        {
            services.AddControllers();
        }

        // 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.UseRouting();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });
        }
    }

ReusableAPI/Controllers/TestController.cs

    [Route("api/[controller]")]
    [ApiController]
    [IgnoreAntiforgeryToken]
    public class TestController:ControllerBase
    {
        [HttpGet]
        public ActionResult Test()
        {
            return Ok("test");
        }
    }

If I run Module, I can call the API controller Test method with no problems
But, if I run the CMS, I can't call the API controller Test method, it gives me a 404 not found instead.

However - If I add controllers support on CMS/Startup.cs, the Reusable API works!

    public class Startup
    {
        // 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 void ConfigureServices(IServiceCollection services)
        {
            services.AddControllers();
            services.AddOrchardCms();
        }

        // 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.UseRouting();
            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });
            app.UseOrchardCore();
        }
    }

enter image description here

But now I can't access the admin page
enter image description here

ns8482e commented 2 years ago

You doesn’t seems to have valid startup in module, module startup inherits from StartupBase

atorres16 commented 2 years ago

Thank you, post edited, tested with same results

ns8482e commented 2 years ago

I still can't find it valid - verify your module startup inherited from Orchard's StartupBase and Configure method has three parameters e.g. https://github.com/OrchardCMS/OrchardCore/blob/ce045393f797fa478795a60aef8425a1d5ed3e85/src/OrchardCore.Modules/OrchardCore.Demo/Startup.cs#L42

atorres16 commented 2 years ago

I didn't know you had to inherit from startupbase, I didn't see that in the videos. I feel I need to configure something, but I have no idea what

jtkech commented 2 years ago

We now support raw startups that don't inherit from StartupBase : IStartup and that can have different signatures

But the Configure() parameter types we are looking for and then use if they exist, are IServiceProvider, IApplicationBuilder and IEndpointRouteBuilder, so you can't use IWebHostEnvironment, you need to resolve it from the passed IServiceProvider instead.