Skip to content

Instantly share code, notes, and snippets.

View Startup.cs
services.AddMvc().AddRazorPagesOptions(options =>
{
options.Conventions.Add(new PageRouteTransformerConvention(new HyphenateRouteParameterTransformer()));
});
View HyphenateRouteParameterTransformer.cs
using Microsoft.AspNetCore.Routing;
using System.Text.RegularExpressions;
public class HyphenateRouteParameterTransformer : IOutboundParameterTransformer
{
public string TransformOutbound(object value)
{
if (value == null)
{
return null;
View ProductService.cs
public interface IProductService
{
List<string> GetProductNames();
}
public class ProductService : IProductService
{
public List<string> GetProductNames()
{
return new List<string>
View ProductConstraint.cs
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)
View DemoEmailService.cs
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())
{
View IEmailService.cs
using System.Threading.Tasks;
namespace RazorPartialToString.Services
{
public interface IEmailService
{
Task SendAsync(string email, string name, string subject, string body);
}
}
View _ContactEmailPartial.cshtml
@model ContactForm
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<title>Contact Email</title>
<style>
body{font-family:Calibri, sans-serif;font-size: 11pt;color: #555;}
View Contact.cshtml
@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>
View Contact.cshtml.cs
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
View Startup.cs
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Latest);
services.AddTransient<IEmailService, DemoEmailService>();
services.AddTransient<IRazorPartialToStringRenderer, RazorPartialToStringRenderer>();