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.36k stars 2.37k forks source link

Routing Issues with Decoupled CMS #2549

Closed mdockal closed 5 years ago

mdockal commented 5 years ago

I was working through @sebastienros' video on Using Orchard Core as a Decoupled CMS. I created a new site using the blank template. I used the blank recipe and started using Razor Pages just like Sebastian did in his demo. I even learned how to add a PageModel to the Page to make separate the code from Page (Index.cshtml.cs). However, when I tried the part where I was trying to pass the route "project-one" to the page I found the same annoying bug of it not finding the layout page. This seems to be an issue in .NET Core not in Orchard Core. So it got me to thinking that I will just replace the Razor Pages with Views and Controllers to get around the bug. However, I can't seem to make orchard find my Home / Index page from the Default Route. I can make it work by typing in the area name. It's like Orchard CMS takes over the routing with no way to add my own homepage unless I use Razor Pages. I can do this with an SaaS app. I thought the OrchardCore.HomeRoute CMS Module was the culprit be apparently not. I am not sure of the ramifications of disabling this module.

jtkech commented 5 years ago

Some things have changed around this and some issues have been fixed, so it depends on the version you use, dev or master.

If you define a @page at the app level the route is /YourPage and becomes the home if the name is Index. From a module (or theme) the route is YourModule/YourPage or /YourModule for an index page.

About overriding routes in your startup and how layouts are defined and found, see under /test/OrchardCore.Tests.Pages, there is a module defining pages, a theme and an app. Build and run the app, do e.g a saas setup, enable Module.Pages, and Theme.Pages depending on what you are testing.

About mvc controller view, yes the app behaves as a module whose area is the app name. To override routes you need the same as with another module. 1) use a MapAreaRoute() in the module startup. But the app's module has no startup so you need to use an helper in your app startup (see below). 2) Or decorate your controller actions with route attributes. See examples in the OC.Demo module.

        services.AddOrchardCms(b =>
        {
            b.Configure((app, routes, serviceProvider) =>
            {
                routes.MapAreaRoute(...);
            });
        });
remesq commented 5 years ago

@jtkech Thank you for that insight, because it helped me to figure out how the heck the pages were being loaded (DUH!). Does the definition have to be @page "{name?}" at the top of the .cshtml file? Or will @page alone at the top of Foo.cshtml be served automatically under /Foo (seems to be the case)?

Also, I see in Startup.cs you can define a homepage thusly:

//This declaration would define an home page
//options.Conventions.AddAreaPageRoute("Module.Pages", "/Foo", "");

I assume it's hardcoded that the "" = index.cshtml. Is it possible to change the name of the file that's served as the home page, or are we locked into index.cshtml?

Thanks!

jtkech commented 5 years ago

It is just to say that the relative part (from the root) is empty so it is the home, for this last parameter you can't have a starting / as for an absolute url. In the above example this is Foo that becomes the home.

There are different ways to make a module page the home.

{name?} is not required, it is used to have parameter(s) which are part of the url. E.g using Foo/John, in the context of the Foo page, the property Name will be equal to John. Then, the ? says that the parameter is optional.

psijkof commented 5 years ago

Another example can be found in the repo under my account here, ModernBusiness.OC.RazorPages

LatinaAtanasova commented 5 years ago

So in case when we have @page "{name}", how can we render "_Layout". The page let's call it Blog is in Pages and the "_Layout" is in Views/Shared. I see that the following url is generated Blog/name and obviously it cannot find Layout and all the files in root folder. Because even if i copy the whole html in the page it doesn't render anything. All of the pages are in the Web Project. Thanks :)

LatinaAtanasova commented 5 years ago

Well, it is fine now.