Skip to content

Instantly share code, notes, and snippets.

services.AddMvc().AddRazorPagesOptions(options =>
{
options.Conventions.Add(new PageRouteTransformerConvention(new HyphenateRouteParameterTransformer()));
});
using Microsoft.AspNetCore.Routing;
using System.Text.RegularExpressions;
public class HyphenateRouteParameterTransformer : IOutboundParameterTransformer
{
public string TransformOutbound(object value)
{
if (value == null)
{
return null;
public interface IProductService
{
List<string> GetProductNames();
}
public class ProductService : IProductService
{
public List<string> GetProductNames()
{
return new List<string>
public class ProductConstraint : IRouteConstraint
{
private readonly IProductService productService;
public ProductConstraint(IProductService productService)
{
this.productService = productService;
}
public bool Match(HttpContext httpContext, IRouter route, string routeKey, RouteValueDictionary values, RouteDirection routeDirection)
using System.Net.Mail;
using System.Threading.Tasks;
namespace RazorPartialToString.Services
{
public class DemoEmailService : IEmailService
{
public async Task SendAsync(string email, string name, string subject, string body)
{
using (var smtp = new SmtpClient())
{
using System.Threading.Tasks;
namespace RazorPartialToString.Services
{
public interface IEmailService
{
Task SendAsync(string email, string name, string subject, string body);
}
}
@model ContactForm
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<title>Contact Email</title>
<style>
body{font-family:Calibri, sans-serif;font-size: 11pt;color: #555;}
@page
@model RazorPartialToString.Pages.ContactModel
@if(TempData["PostResult"] == null)
{
<form method="post">
<label asp-for="ContactForm.Name"></label>
<div class="form-group">
<input asp-for="ContactForm.Name">
</div>
<label asp-for="ContactForm.Email"></label>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using RazorPartialToString.Services;
namespace RazorPartialToString.Pages
{
public class ContactModel : PageModel
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Latest);
services.AddTransient<IEmailService, DemoEmailService>();
services.AddTransient<IRazorPartialToStringRenderer, RazorPartialToStringRenderer>();