{ public Startup(IConfiguration configuration) { Configuration = configuration; }
public IConfiguration Configuration { get; }
public void ConfigureServices(IServiceCollection services)
{
services.AddRazorPages();
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Error");
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapRazorPages();
});
}
}
@page @using RazorPagesIntro.Pages @model Index2Model
@Model.Message
using Microsoft.AspNetCore.Mvc.RazorPages; using Microsoft.Extensions.Logging; using System;
namespace RazorPagesIntro.Pages { public class Index2Model : PageModel { public string Message { get; private set; } = "PageModel in C#";
public void OnGet()
{
Message += $" Server time is { DateTime.Now }";
}
}
}
public void ConfigureServices(IServiceCollection services) { services.AddDbContext(options => options.UseInMemoryDatabase("name")); services.AddRazorPages(); }
using System.ComponentModel.DataAnnotations;
namespace RazorPagesContacts.Models { public class Customer { public int Id { get; set; }
[Required, StringLength(10)]
public string Name { get; set; }
}
}
using Microsoft.EntityFrameworkCore; using RazorPagesContacts.Models;
namespace RazorPagesContacts.Data { public class CustomerDbContext : DbContext { public CustomerDbContext(DbContextOptions options) : base(options) { }
public DbSet<Customer> Customers { get; set; }
}
}
@page @model RazorPagesContacts.Pages.Customers.CreateModel @addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
Enter a customer name:
Name:using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.RazorPages; using RazorPagesContacts.Data; using RazorPagesContacts.Models; using System.Threading.Tasks;
namespace RazorPagesContacts.Pages.Customers { public class CreateModel : PageModel { private readonly CustomerDbContext _context;
public CreateModel(CustomerDbContext context)
{
_context = context;
}
public IActionResult OnGet()
{
return Page();
}
[BindProperty]
public Customer Customer { get; set; }
public async Task<IActionResult> OnPostAsync()
{
if (!ModelState.IsValid)
{
return Page();
}
_context.Customers.Add(Customer);
await _context.SaveChangesAsync();
return RedirectToPage("./Index");
}
}
}
public async Task OnPostAsync() { if (!ModelState.IsValid) { return Page(); }
_context.Customers.Add(Customer);
await _context.SaveChangesAsync();
return RedirectToPage("./Index");
}
@page @model RazorPagesContacts.Pages.Customers.CreateModel @addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
Enter a customer name:
Name:Enter a customer name:
Name:public async Task OnPostAsync() { if (!ModelState.IsValid) { return Page(); }
_context.Customers.Add(Customer);
await _context.SaveChangesAsync();
return RedirectToPage("./Index");
}
public class CreateModel : PageModel { private readonly CustomerDbContext _context;
public CreateModel(CustomerDbContext context)
{
_context = context;
}
public IActionResult OnGet()
{
return Page();
}
[BindProperty]
public Customer Customer { get; set; }
public async Task<IActionResult> OnPostAsync()
{
if (!ModelState.IsValid)
{
return Page();
}
_context.Customers.Add(Customer);
await _context.SaveChangesAsync();
return RedirectToPage("./Index");
}
[BindProperty(SupportsGet = true)]
@page @model RazorPagesContacts.Pages.Customers.CreateModel @addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
Enter a customer name:
Name:@page @model RazorPagesContacts.Pages.Customers.IndexModel @addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
@foreach (var contact in Model.Customer) { }ID | Name | |
---|---|---|
@contact.Id | @contact.Name | Edit | delete |
public class IndexModel : PageModel { private readonly CustomerDbContext _context;
public IndexModel(CustomerDbContext context)
{
_context = context;
}
public IList<Customer> Customer { get; set; }
public async Task OnGetAsync()
{
Customer = await _context.Customers.ToListAsync();
}
public async Task<IActionResult> OnPostDeleteAsync(int id)
{
var contact = await _context.Customers.FindAsync(id);
if (contact != null)
{
_context.Customers.Remove(contact);
await _context.SaveChangesAsync();
}
return RedirectToPage();
}
}
Edit | <button type="submit" asp-page-handler="delete"
delete
public async Task OnPostDeleteAsync(int id) { var contact = await _context.Customers.FindAsync(id);
if (contact != null)
{
_context.Customers.Remove(contact);
await _context.SaveChangesAsync();
}
return RedirectToPage();
}
@page "{id:int}" @model RazorPagesContacts.Pages.Customers.EditModel @addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
<div>
<button type="submit">Save</button>
</div>
@page "{id:int?}"
public class EditModel : PageModel { private readonly CustomerDbContext _context;
public EditModel(CustomerDbContext context)
{
_context = context;
}
[BindProperty]
public Customer Customer { get; set; }
public async Task<IActionResult> OnGetAsync(int id)
{
Customer = await _context.Customers.FindAsync(id);
if (Customer == null)
{
return RedirectToPage("./Index");
}
return Page();
}
public async Task<IActionResult> OnPostAsync()
{
if (!ModelState.IsValid)
{
return Page();
}
_context.Attach(Customer).State = EntityState.Modified;
try
{
await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
throw new Exception($"Customer {Customer.Id} not found!");
}
return RedirectToPage("./Index");
}
}
@page @model RazorPagesContacts.Pages.Customers.CreateModel @addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
Validation: customer name:
Name: <script src="~/lib/jquery/dist/jquery.js"></script> <script src="~/lib/jquery-validation/dist/jquery.validate.js"></script> <script src="~/lib/jquery-validation-unobtrusive/jquery.validate.unobtrusive.js"></script>Enter a customer name:
Name: <script src="/lib/jquery/dist/jquery.js"></script> <script src="/lib/jquery-validation/dist/jquery.validate.js"></script> <script src="/lib/jquery-validation-unobtrusive/jquery.validate.unobtrusive.js"></script>using System; using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema;
namespace RazorPagesMovie.Models { public class Movie { public int ID { get; set; }
[StringLength(60, MinimumLength = 3)]
[Required]
public string Title { get; set; }
[Display(Name = "Release Date")]
[DataType(DataType.Date)]
public DateTime ReleaseDate { get; set; }
public void OnHead()
{ HttpContext.Response.Headers.Add("Head Test", "Handled by OnHead!"); }
<title>RP Sample</title> Home Create Customers@RenderBody()
<script src="~/lib/jquery/dist/jquery.js"></script>
<script src="~/lib/jquery-validation/dist/jquery.validate.js"></script>
<script src="~/lib/jquery-validation-unobtrusive/jquery.validate.unobtrusive.js"></script>
@{ Layout = "_Layout"; {
@page @namespace RazorPagesIntro.Pages.Customers
@model NameSpaceModel
@Model.Message
@page @model CreateModel
Enter a customer name:
Name:public class CreateModel : PageModel { private readonly CustomerDbContext _context;
public CreateModel(CustomerDbContext context)
{
_context = context;
}
public IActionResult OnGet()
{
return Page();
}
[BindProperty]
public Customer Customer { get; set; }
public async Task<IActionResult> OnPostAsync()
{
if (!ModelState.IsValid)
{
return Page();
}
_context.Customers.Add(Customer);
await _context.SaveChangesAsync();
return RedirectToPage("./Index");
}
}
RedirectToPage("/Index", new { area = "Services" });
public class AboutModel : PageModel { [ViewData] public string Title { get; } = "About";
public void OnGet()
{
} }
public class CreateDotModel : PageModel { private readonly AppDbContext _db;
public CreateDotModel(AppDbContext db)
{
_db = db;
}
[TempData]
public string Message { get; set; }
[BindProperty]
public Customer Customer { get; set; }
public async Task<IActionResult> OnPostAsync()
{
if (!ModelState.IsValid)
{
return Page();
}
_db.Customers.Add(Customer);
await _db.SaveChangesAsync();
Message = $"Customer {Customer.Name} added";
return RedirectToPage("./Index");
}
}
[TempData] public string Message { get; set; }
@page @model CreateFATHModel
Enter your name.
using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.RazorPages; using RazorPagesContacts.Data;
namespace RazorPagesContacts.Pages.Customers { public class CreateFATHModel : PageModel { private readonly AppDbContext _db;
public CreateFATHModel(AppDbContext db)
{
_db = db;
}
[BindProperty]
public Customer Customer { get; set; }
public async Task<IActionResult> OnPostJoinListAsync()
{
if (!ModelState.IsValid)
{
return Page();
}
_db.Customers.Add(Customer);
await _db.SaveChangesAsync();
return RedirectToPage("/Index");
}
public async Task<IActionResult> OnPostJoinListUCAsync()
{
if (!ModelState.IsValid)
{
return Page();
}
Customer.Name = Customer.Name?.ToUpperInvariant();
return await OnPostJoinListAsync();
}
}
}
@page "{handler?}" @model CreateRouteModel
Enter your name.
public void ConfigureServices(IServiceCollection services)
{
services.AddRazorPages(options =>
{
options.RootDirectory = "/MyPages";
options.Conventions.AuthorizeFolder("/MyPages/Admin");
});
}
public void ConfigureServices(IServiceCollection services)
{
services.AddRazorPages(options =>
{
options.Conventions.AuthorizeFolder("/MyPages/Admin");
})
.WithRazorPagesAtContentRoot();
}
public void ConfigureServices(IServiceCollection services)
{
services.AddRazorPages(options =>
{
options.Conventions.AuthorizeFolder("/MyPages/Admin");
})
.WithRazorPagesRoot("/path/to/razor/pages");
}
@using System.Net.Http @using Microsoft.AspNetCore.Authorization @using Microsoft.AspNetCore.Components.Authorization @using Microsoft.AspNetCore.Components.Forms @using Microsoft.AspNetCore.Components.Routing @using Microsoft.AspNetCore.Components.Web @using Microsoft.JSInterop @using MyAppNamespace
services.AddServerSideBlazor();
endpoints.MapBlazorHub();
@using Microsoft.AspNetCore.Components.Routing
Sorry, but there's nothing here!
@page "/blazor" @{ Layout = "_Layout"; }
app.UseEndpoints(endpoints => { ...
endpoints.MapFallbackToPage("/_Host");
});
@page "/counter"
...
@using Microsoft.AspNetCore.Components.Routing
Sorry, but there's nothing here!
@{ Layout = "_Layout"; }
public IActionResult Blazor() { return View("_Host"); }
app.UseEndpoints(endpoints => { ...
endpoints.MapFallbackToController("Blazor", "Home");
});
@page "/counter"
...
@functions { [BindProperty(SupportsGet=true)] public int InitialValue { get; set; } }
Set initial value@functions { [BindProperty(SupportsGet=true)] public int InitialValue { get; set; } }
@using MyAppNamespace.Components