Skip to content

Razor Pages Examples

Stephan van Leeuwen edited this page May 3, 2021 · 9 revisions

Usage Examples for Razor Pages Apps

Simple razor page

/Pages/Index.cshtml

@page
<h1>Index</h1>

Usage

@Url.Pages().Index() // => "/"

Razor page with route parameters

/Pages/Details.cshtml

@page "{id:int}"
<h1>Detail</h1>

Usage

@Url.Pages().Detail(id: 2) // => "/Detail/2"

Razor page with model

/Pages/Model.cshtml

@page
@model AspNetCoreSamplePages.Model
<h1>Model</h1>

/Pages/Model.cshtml.cs

namespace AspNetCoreSamplePages
{
    public class Model : PageModel
    {
        public void OnGet(int page)
        {
            //...
        }
    }
}

Usage

@Url.Pages().Model(page: 2) // => "/Model?page=2"

Page with specific handler

/Pages/Index.cshtml

@page
@model AspNetCoreSamplePages.IndexModel
<h1>Handler</h1>

/Pages/Index.cshtml.cs

namespace AspNetCoreSamplePages
{
    public class IndexModel : PageModel
    {
        public void OnGet()
        {
        }

        public void OnGetSpecific()
        {
        }
    }
}

Usage

@Url.Pages().IndexHandlers.Specific() // => "/?handler=Specific"

Page in an area

/Areas/Admin/Pages/Index.cshtml

@page
<h1>Admin Area</h1>

Usage

@Url.AdminPages().Index() // => "/Admin/"
Clone this wiki locally