Coder Social home page Coder Social logo

osnova's Introduction

Osnova

A minimalistic framework for making static websites with ASP.NET Core and Razor Pages. It lets developers use their existing Razor Pages skills to quickly turn their web apps into static sites, without being limited by predefined templates.

Getting Started

Step 1: Create your project

Start by creating a new ASP.NET Core Razor Pages project if you don’t have one set up already. Use .NET CLI or follow this guide.

dotnet new razor

Step 2: Install Osnova

Install Osnova from the .NET CLI as:

dotnet add package Osnova

Or from the package manager console:

PM> Install-Package Osnova

Use the --version option to specify a preview version to install.

Step 3: Configure your services

To register Osnova services, open Program.cs, and add a call to a builder.Services.AddOsnova() after builder.Services.AddRazorPages() call:

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddRazorPages();
builder.Services.AddOsnova();

Step 4: Start adding static pages to your project

Let's modify default Pages\Index page to be static. Replace OnGet with OnGetStatic method handler.

public class IndexModel : PageModel
{
    public string Message { get; set; }

    public void OnGetStatic()
    {
        Message = "Hello static message!";
    }
}

Update Index.cshtml template.

@page 
@model IndexModel 
@{ 
    ViewData["Title"] = "Home page"; 
}

<h1>@Model.Message</h1>

You can serve the site locally by running app for example with CLI command:

dotnet watch run

Step 5: Export static content

To export an application to static files run it with export parameter using CLI command:

dotnet run -- export

This will export the content of wwwroot folder and rendered static pages to output folder in your project directory.

Congratulations, you are ready to deploy your Osnova application to production with the hosting of your choice.

Static Razor Pages

OnGetStatic

Only if your PageModel contains a handler method called OnGetStatic, Osnova will render such page to output during export with data returned by handler.

public class IndexModel : PageModel
{
    public string Message { get; set; }

    public void OnGetStatic()
    {
        Message = "Hello static message!";
    }
}

It is equivalent to OnGet method handler and can have routing parameters as arguments. In the same way OnGetStatic must be public and can return void, Task if asynchronous, or an IActionResult (or Task<IActionResult>). You cannot have both OnGet and OnGetStatic on the same page.

GetStaticPaths

Routes with parameters in Razor Pages that use OnGetStatic handler must include a list of paths to be statically generated. Osnova will statically render page with all the paths specified by GetStaticPaths.

// @page "/article/{id}"

public class ArticleModel : PageModel
{
    public string ArticleContent { get; set; }

    // Generates `/article/1` and `/article/2`
    public StaticPaths GetStaticPaths()
    {
        return new StaticPaths
        {
            Paths = new []
            {
                new {id = 1},
                new {id = 2},
            }
        };
    }

    // Use of `GetStaticPaths` requires using `OnGetStatic`
    public void OnGetStatic(int id)
    {
        // Load article content with passed id as route param
        // ArticleContent = _articlesService.LoadArticleContentById(id);
    }
}

osnova's People

Contributors

rozumak avatar

Stargazers

MykolaKushnarenko avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.