Netcore Razor Pages

So I’ve been reviewing the net core razor template and noticed that everything is located inside the wwwroot folder without a @page directive.

What I’m looking to do is integrate ServiceStack into standard Razor Pages within the Pages folder.

At the moment if I use the @page directive, alongside the _viewimport @inherits ViewPage I get the following error:

The name ‘PageContext’ does not exist in the current context.

Any guidance?

ServiceStack’s razor template uses MVC Razor .cshtml pages but Blazor .razor Component pages uses the @page directive for routing, you can create a Blazor Server template from:

https://servicestack.net/blazor#create

Hi Mythz,

I’m not using Blazor, just Razor Pages… As per below.

And this utilises the @page directive turning the page into an MVC action.

Dan

The only server Razor templates we have are razor and the MVC Project templates, you can try starting with mvc to see if you can configure it to also use Razor pages, otherwise you can request a feature request for a new Razor Pages project template at:

https://servicestack.net/ideas

I did some further diving on this, and I’ve worked out why and also a workaround.

ViewPage currently inherits from RazorPage, which supports cshtml in general and not Razor Pages specifically.

Ref: https://github.com/aspnet/Mvc/issues/6769

I’ve created a version of ViewPage (RazorViewPage) that derives from Microsoft.AspNetCore.Mvc.RazorPages.Page instead, and this only required a few tweaks to get working.

Now the ‘The name ‘PageContext’ does not exist in the current context’ error is gone.

Happy to throw in a PR showing what I have done if it might be useful to somebody else, I would also be interested to see how this could be integrated into ServiceStack.Mvc.

1 Like

Cool, if you can submit a PR somewhere so I can see the changes I can look into how to best support it, we may need multiple base classes.

To support Razor Pages properly required a few changes in ServiceStack which is now available from v6.5.1+ on MyGet.

I’ve also created a new razor-pages template containing an integrated example configured with Auth, Registration, form binding, validation, CRUD & Todo MVC example.

$ x new razor-pages ProjectName

It’s also using our new modern JS script module support which takes advantage of browsers native module support which enables modern JS development without any npm tooling that we’re moving other templates to use. To update the typed DTOs you can update the x dotnet-tool and update dto.mjs with:

$ x mjs