Skip to content

Instantly share code, notes, and snippets.

View Pagination.cshtml.cs
public class PaginationModel : PageModel
{
[BindProperty(SupportsGet = true)]
public int CurrentPage { get; set; } = 1;
public int Count { get; set; }
public int PageSize { get; set; } = 10;
public int TotalPages => (int)Math.Ceiling(decimal.Divide(Count, PageSize));
}
View RazorEngineViewOptionsFileProviders.Startup.cs
public void ConfigureServices(IServiceCollection services)
{
// Add framework services.
services.AddMvc();
services.Configure<RazorViewEngineOptions>(opts =>
opts.FileProviders.Add(
new DatabaseFileProvider(Configuration.GetConnectionString("DefaultConnection"))
)
);
}
View DatabaseFileProvider.cs
using Microsoft.Extensions.FileProviders;
using Microsoft.Extensions.Primitives;
using System;
using System.IO;
namespace RazorEngineViewOptionsFileProviders
{
public class DatabaseFileProvider : IFileProvider
{
private string _connection;
View Pagination.cshtml.cs
public class PaginationModel : PageModel
{
private readonly IPersonService _personService;
public PaginationModel(IPersonService personService)
{
_personService = personService;
}
[BindProperty(SupportsGet = true)]
View Pagination.cshtml
@page
@model PaginationModel
<h2>Pagination Example</h2>
<table class="table table-striped">
@foreach (var item in Model.Data)
{
<tr>
<td>@item.Id</td>
View Index.cshtml
@page
@model IndexModel
@{
ViewData["Title"] = "Home page";
}
<div>
@Model.Message
</div>
<form method="post">
<div class="form-group">
View Index.cshtml.cs
using AuthenticationSample.Models;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.Extensions.Configuration;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Security.Claims;
View SiteUser.cs
namespace AuthenticationSample.Models
{
public class SiteUser
{
public string UserName { get; set; }
public string Password { get; set; }
}
}
View appSettings.json
{
"Logging": {
"LogLevel": {
"Default": "Warning"
}
},
"AllowedHosts": "*",
"SiteUser": {
"UserName": "your user name",
"Password": "your hashed password"
View Startup.cs
public void ConfigureServices(IServiceCollection services)
{
services.Configure<CookiePolicyOptions>(options =>
{
// This lambda determines whether user consent for non-essential cookies is needed for a given request.
options.CheckConsentNeeded = context => true;
options.MinimumSameSitePolicy = SameSiteMode.None;
});
services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme).AddCookie(cookieOptions => {
cookieOptions.LoginPath = "/";