16

Say I have 2 pages

  1. /NotADefault.cshtml

  2. /Views/Default.cshtml

Question 1.

Now I run it, page A always gets called implicitly as start-up default page no matter what I name it.
Page B will only be called when I explicitly call localhost/View/Default. How do I make page B (the one in View folder) my default page?

Question 2.

I also have NotADefaultService.cs and DefaultService.cs. I give each page a Service class at the back. However, when page A is called NotADefaultService.cs never gets called. Only DefaultService.cs gets called when page B is called...

My observation is that only the pages in the View folder will get their back-end service class working. Outside of View folder it doesn't work.

Combining Q1 and Q2.

How do I:

Option 1. get the backend service class working under / root outside "View" folder?

OR

Option 2. appoint /View/Default.schtml as my default at start-up where the service class can be hit?

Tom
  • 15,781
  • 14
  • 69
  • 111

1 Answers1

24

In ServiceStack:

  • Razor Pages that exist within the /Views/ folder are called View Pages
  • Razor Pages that exist anywhere else are called Content Pages

The difference between them is that View Pages are Razor views that are used to provide the HTML representations (aka views) for services in much the same way View Pages work for MVC Controllers.

View Pages cannot be called directly, that's the role of Content Pages, which can only be called directly, i.e. outside the context of a service (or redirected to, from a service).

In Razor Rockstars, examples of Content Pages include:

Whereas examples of View Pages include:

Default Pages

For Content Pages the default.cshtml is the index page for a folder. So to set a default page for the root / path, create a /default.cshtml page. An example of this is /default.cshtml home page used in the Reusability demo.

If you want to use a view page as the Home page, you can set the default redirect to it by adding the AppHost config:

SetConfig(new HostConfig { 
   DefaultRedirectPath = "/home"
});

Which would call a service matching the /home route that will use the most appropriate View Page based on the rules laid out in the Razor Rockstars page.

mythz
  • 141,670
  • 29
  • 246
  • 390
  • 1
    When using DefaultRedirectPath is there anything that can be done to not show /home in the address? – Jon Canning Nov 27 '12 at 16:17
  • Nope, it only redirects. You'd need a to impl handle the default request in custom RawHttpHandler or a catch-all to provide a dynamic request for a default route. See [Order of Operations](https://github.com/ServiceStack/ServiceStack/wiki/Order-of-Operations) for details about the custom hooks available. – mythz Nov 27 '12 at 17:05
  • Does this mean that ServiceStack.Razor doesn't follow the MVC pattern (except for View Pages to a certain extent)? – KBoek Feb 23 '13 at 10:56
  • Not except, Your service (the Controller) returns the response (the Model) for Razor pages (the View) in `/Views` directory. Pages outside (i.e. Content Pages) are called directly and do not go through a service. This is essentially what this answer is all about. – mythz Feb 23 '13 at 11:47
  • 8
    Hides the /home – Jon Canning Apr 29 '13 at 10:58
  • I found Jon Canning's answer along with using DefaultRedirectPath the most helpful. Maybe this can be mentioned in the ServiceStack documentation as well. – kinstephen Feb 21 '14 at 03:06
  • Îs this `DefaultPageName` now? –  Jun 22 '15 at 21:13