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.37k stars 2.38k forks source link

Error using Orchard CMS with an MVC Module #5394

Closed ibiza240 closed 4 years ago

ibiza240 commented 4 years ago

Hi!

I am trying really hard to make this work as the framework looks promising. Ultimately, I want to be able to use the CMS backend for managing content and writing my own MVC views and controllers for displaying it.

I am using the Sample projects provided in the solution:

OrchardCore.Mvc.Web OrchardCore.Mvc.HelloWorld

This is showcasing an MVC webapp using Orchard Core, hence I need to add the CMS part to it for what I need.

Here's the original Startup.cs from the OrchardCore.Mvc.Web project, the webapp works perfectly when launching it:

using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

namespace OrchardCore.Mvc.Web
{
    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 http://go.microsoft.com/fwlink/?LinkID=398940
        public void ConfigureServices(IServiceCollection services)
        {
            // Add ASP.NET MVC and support for modules
            services
                .AddOrchardCore()
                .AddMvc()
                ;
        }

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

            app.UseStaticFiles();

            app.UseOrchardCore();
        }
    }
}

So all that is missing is the CMS, right? Here's what I tried:

  1. Add the OrchardCore.Application.Cms.Targets nuget package reference to the OrchardCore.Mvc.Web project
  2. Add the CMS service by adding .AddOrchardCms() to the IServiceCollection in ConfigureServices above

Then I run the webapp again and am greeted with the Setup page for the CMS. Cool! I complete the setup and then, when it goes back to the home page, I now get a page displaying the "The page could not be found." message...

So it seems it is not recognizing anymore the Hello world module :(

What am I missing? Thanks!

ns8482e commented 4 years ago

@ibiza240 It seems recipe that you used to setup doesn't have default home page. Access /admin and create content item that you want to set it as home page.

ibiza240 commented 4 years ago

Thanks for your reply @ns8482e!

The recipe I picked is Blank and you are right that I have no content items but I don't think this is the problem - I don't want a content item as the home page, rather a custom MVC view (I am testing with the "Hello World" sample module).

Here is the Startup.cs from the OrchardCore.Mvc.HelloWorld project (the sample module):

using System;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Routing;
using Microsoft.Extensions.Configuration;
using OrchardCore.Modules;

namespace OrchardCore.Mvc.HelloWorld
{
    public class Startup : StartupBase
    {
        private readonly IConfiguration _configuration;

        public Startup(IConfiguration configuration)
        {
            _configuration = configuration;
        }

        public override void Configure(IApplicationBuilder builder, IEndpointRouteBuilder routes, IServiceProvider serviceProvider)
        {
            if (string.IsNullOrEmpty(_configuration["Sample"]))
            {
                throw new Exception(":(");
            }

            routes.MapAreaControllerRoute
            (
                name: "Home",
                areaName: "OrchardCore.Mvc.HelloWorld",
                pattern: "",
                defaults: new { controller = "Home", action = "Index" }
            );
        }
    }
}

I did not change it at all, and it was working great before activating CMS. Basically it re-routes the home page (root url) to the Home controller in the sample module, so that "Hello World" is displayed directly from the root url.

I would expect this to still be working after activating the CMS on the website project. That's what is confirming that MVC is working well, which is not for me after activating the CMS...in fact if I put a breakpoint in that Configure function, it's not hit anymore on startup, after activating the CMS.

Skrypt commented 4 years ago

If you picked up the blank recipe then there is no homepage set. This is intentional as some people are using Orchard Core as a API server for their headless websites. This means that the only thing they require from Orchard Core is to have a /admin (or now you can set this to / as the admin url is now configurable). So it is working as it should and as expected. If you want a home page. You can still create one but you will need to configure it in your OC database because Orchard Core has a default route for the homepage which is configurable in the SiteSettings in the Document table.

see : https://github.com/OrchardCMS/OrchardCore/blob/dev/src/OrchardCore.Modules/OrchardCore.Settings/SiteSettings.cs#L29

ibiza240 commented 4 years ago

some people are using Orchard Core as a API server for their headless websites. This means that the only thing they require from Orchard Core is to have a /admin

This is called "Decoupled CMS", right? That is exactly what I want to achieve...

The gist of my problem is not that the home page is not set, but rather that the Hello World module is not recognized anymore after I activated the CMS in the Startup class of the website...

Anyone can help me identify what is incorrect in my setup?

Skrypt commented 4 years ago

Are you using source code or Nuget package solution? Because if you are using source code you need to add your module reference to the https://github.com/OrchardCMS/OrchardCore/blob/dev/src/OrchardCore/OrchardCore.Application.Cms.Targets/OrchardCore.Application.Cms.Targets.csproj Else you add the reference to your website project.

ibiza240 commented 4 years ago

I am using Nuget package and the module is referenced from the website project

image

Skrypt commented 4 years ago

Does it have a manifest.cs file?

ibiza240 commented 4 years ago

The module has the Manifest file from the sample project, I did not modify it:

using OrchardCore.Modules.Manifest;

[assembly: Module(
    Name = "Mvc HelloWorld"
)]
Skrypt commented 4 years ago

Is that module enabled in the features?

/Admin/Features

ibiza240 commented 4 years ago

A priori, I would say you probably found the problem since I did not enable the module in the Admin backend.

But now I can't login into the Admin, when I login I get back to the login page infinitely.

I will try to solve that problem now, and report back here :)

Skrypt commented 4 years ago

This can be a cookie issue. Just delete all your cookies and retry. I get this issue often.

ibiza240 commented 4 years ago

Thanks @Skrypt, activating the module in the CMS is what I was missing!

jaypeemayo commented 3 years ago

@ibiza240 I had the same problem as yours and @Skrypt answer saves us. thanks