Saturday, October 6, 2018

RazorInterfaces: interfaces for the standard HTTP methods

In playing around with Razor Pages, I was irritated again that Microsoft couldn't just standardize a set of interfaces for their methods so that they wouldn't need so much magic.

So I just quickly hacked up RazorInterfaces, available also on nuget.org. It's probably most useful to people just learning Razor Pages, since it requires you to correctly implement the common HTTP methods you'll need to get things running:

public class CustomerPage : PageModel
                          , IPageGet
                          , IPagePostAsync<Customer>
{
 public IActionResult OnGet()
 {
  return Page();
 }

 public async Task<IActionResult> OnPostAsync(Customer customer)
 {
  await DataLayer.Update(customer);
  return Page();
 }
}

There are interfaces for all of the standard HTTP methods: GET, POST, PUT, DELETE, HEAD, OPTIONS. The interface names conform to the following format: IPage[HTTP method] and IPage[HTTP method]Async for the async variant, and there are variants also accepting a single type parameter, IPage[HTTP method]<T> and IPage[HTTP method]Async<T>.