Skip to content

Commit

Permalink
Auto stash before merge of "master" and "origin/master"
Browse files Browse the repository at this point in the history
  • Loading branch information
ardalis committed Sep 22, 2017
1 parent f68a315 commit f15f2cf
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 7 deletions.
6 changes: 3 additions & 3 deletions WithRazorPages/Pages/Ninjas/Index.cshtml
Expand Up @@ -6,7 +6,7 @@
}

<h2>@ViewBag.ItemNames</h2>

<h3>Razor Pages</h3>
<table class="table">
<tr>
<th>Name</th>
Expand All @@ -16,10 +16,10 @@
{
<tr>
<td><a asp-page="Details" asp-route-id="@item.Id">@item</a></td>
<td><form asp-page-handler="Delete" asp-route-id="@item.Id"><input type="submit" value="Delete" class="btn btn-link" /></form></td>
<td><form asp-page-handler="Delete" asp-route-id="@item.Id" method="post"><input type="submit" value="Delete" class="btn btn-link" /></form></td>
</tr>
}
</table>
<form asp-page-handler="Add"><input type="submit" value="Add @ViewBag.ItemName" class="btn btn-success" /></form><br />
<form asp-page-handler="Add" method="post"><input type="submit" value="Add @ViewBag.ItemName" class="btn btn-success" /></form><br />
<a asp-page="Swords/Index">Ninja Swords List</a>

1 change: 1 addition & 0 deletions WithRazorPages/Pages/Ninjas/Index.cshtml.cs
Expand Up @@ -49,6 +49,7 @@ public async Task<IActionResult> OnPostAddAsync()
return RedirectToPage();
}

[PositiveParameter("id")]
public async Task<IActionResult> OnPostDeleteAsync(int id)
{
var entityToDelete = _ninjaRepository.GetById(id);
Expand Down
2 changes: 1 addition & 1 deletion WithRazorPages/Pages/Pirates/Index.cshtml.cs
@@ -1,5 +1,4 @@
using Microsoft.AspNetCore.Mvc.RazorPages;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
Expand All @@ -8,6 +7,7 @@

namespace WithRazorPages.Pages.Pirates
{
[ValidateModel]
public class IndexModel : PageModel
{
private readonly IRepository<Pirate> _pirateRepository;
Expand Down
4 changes: 2 additions & 2 deletions WithRazorPages/Pages/Zombies/Index.cshtml
Expand Up @@ -28,7 +28,7 @@

public async Task OnGetAsync()
{
Zombies = _zombieRepository.List();
Zombies = _zombieRepository.List()
.Select(n => new ZombieViewModel { Id = n.Id, Name = n.Name }).ToList();
}

Expand Down Expand Up @@ -57,5 +57,5 @@
<li><a asp-page="/@ViewBag.ItemNames/Details" asp-route-id="@item.Id">@item</a></li>
}
</ul>
<a asp-page="Index" asp-page-handler="Add">Add @ViewBag.ItemName</a><br />
<a asp-page="Index" asp-route-handler="Add">Add @ViewBag.ItemName</a><br />

96 changes: 95 additions & 1 deletion WithRazorPages/Startup.cs
Expand Up @@ -6,6 +6,12 @@
using WithRazorPages.Core.Interfaces;
using WithRazorPages.Infrastructure.Data;
using Microsoft.EntityFrameworkCore;
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.AspNetCore.Mvc;
using System;
using Microsoft.Extensions.Logging;
using Microsoft.AspNetCore.Mvc.RazorPages;
using System.Linq;

namespace WithRazorPages
{
Expand All @@ -31,8 +37,8 @@ public void ConfigureServices(IServiceCollection services)
})
.AddRazorPagesOptions(rpopt =>
{
// this option should become available at RTM
rpopt.Conventions.AddPageRoute("/Pages/Shared/Error", "/Error");
rpopt.Conventions.ConfigureFilter(new SelectionLoggingFilter());
});

services.AddScoped<IRepository<Ninja>, EfRepository<Ninja>>();
Expand Down Expand Up @@ -63,4 +69,92 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env)
});
}
}

public class ValidateModelAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext context)
{
if (!context.ModelState.IsValid)
{
context.Result = new BadRequestObjectResult(context.ModelState);
}
}
}

public class HandlerChangingPageFilterAttribute : Attribute, IPageFilter
{
public void OnPageHandlerSelected(PageHandlerSelectedContext context)
{
context.HandlerMethod = context.ActionDescriptor.HandlerMethods.First(m => m.Name == "Edit");
}

public void OnPageHandlerExecuting(PageHandlerExecutingContext context)
{
}

public void OnPageHandlerExecuted(PageHandlerExecutedContext context)
{
}
}

public class SelectionLoggingFilter : IPageFilter
{
public void OnPageHandlerExecuted(PageHandlerExecutedContext context)
{
}

public void OnPageHandlerExecuting(PageHandlerExecutingContext context)
{
}

public void OnPageHandlerSelected(PageHandlerSelectedContext context)
{
var factory = context.HttpContext.RequestServices.GetRequiredService<ILoggerFactory>();
var logger = factory.CreateLogger(context.HandlerInstance.ToString());
var instance = context.HandlerInstance as PageModel;
if (instance != null)
{
logger.LogWarning(instance.PageContext.ActionDescriptor.DisplayName);
context.HandlerInstance.
logger.LogWarning($"{context.HandlerInstance.GetType().ToString()} selected.");
}
}
}
public class PositiveParameterAttribute : Attribute, IPageFilter
{
private readonly string _parameterName;

public PositiveParameterAttribute(string parameterName)
{
_parameterName = parameterName;
}

public void OnPageHandlerExecuted(PageHandlerExecutedContext context)
{
}

public void OnPageHandlerExecuting(PageHandlerExecutingContext context)
{
if (!context.HandlerArguments.Keys.Contains(_parameterName))
{
context.Result = new BadRequestObjectResult($"Parameter {_parameterName} missing.");
}
var value = context.HandlerArguments[_parameterName].ToString();
if (int.TryParse(value, out var result))
{
if (result < 0)
{
context.Result = new BadRequestObjectResult($"Parameter {_parameterName} is negative.");
}
}
else
{
context.Result = new BadRequestObjectResult($"Parameter {_parameterName} was not a valid integer.");
}
}

public void OnPageHandlerSelected(PageHandlerSelectedContext context)
{
}
}
}

0 comments on commit f15f2cf

Please sign in to comment.