Wednesday, December 12, 2018

Use Razor for other templates in C#, not just Controller views.

They can be used for email templates and the like. In the code below I use a .cshtml view full of Razor to prep the XML for this API, though be warned the code below only supports the happy pass scenario.

using System;
using System.IO;
using System.Net;
using System.Reflection;
using System.Text;
using System.Xml.Linq;
using SomethingErOther.Core;
using RazorEngine.Templating;
namespace SomethingErOther.DataAccess
{
   public class AddressValidationRepository
   {
      public AddressValidationAPIModel UseService(string street1, string street2, string
            city, string state, string zip, string url, string username)
      {
         TemplateService templateService = new TemplateService();
         AddressValidationXMLModel addressValidationXMLModel = new
               AddressValidationXMLModel(street1, street2, city, state, zip, username);
         string xml = GenerateProgramEmailBody(templateService,
               addressValidationXMLModel);
         return CallApi(String.Format("{0}?API=Verify&XML={1}", url, xml));
      }
      
      private string GenerateProgramEmailBody(TemplateService templateService,
            AddressValidationXMLModel addressValidationXMLModel)
      {
         var template = LoadEmbeddedTemplate();
         return templateService.Parse(template, addressValidationXMLModel, null, "XML");
      }
      
      private string LoadEmbeddedTemplate()
      {
         var assembly = Assembly.GetExecutingAssembly();
         using (var stream = assembly.GetManifestResourceStream(
               "SomethingErOther.DataAccess.AddressValidationTemplates.XML.cshtml"))
         {
            using (var reader = new StreamReader(stream))
            {
               return reader.ReadToEnd();
            }
         }
      }
      
      private AddressValidationAPIModel CallApi(string url)
      {
         using (WebClient webClient = new WebClient())
         {
            byte[] bytes = webClient.DownloadData(url);
            string result = Encoding.UTF8.GetString(bytes);
            return ParseXML(result);
         }
      }
      
      private AddressValidationAPIModel ParseXML(string xml)
      {
         AddressValidationAPIModel addressValidationAPIModel = new
               AddressValidationAPIModel();
         XDocument xmlDocument = XDocument.Parse(xml);
         foreach (XElement outerElement in xmlDocument.Root.Elements())
         {
            foreach (XElement innerElement in outerElement.Elements())
            {
               switch (innerElement.Name.ToString())
               {
                  case "Address1":
                     addressValidationAPIModel.Street2 = innerElement.Value;
                     break;
                  case "Address2":
                     addressValidationAPIModel.Street1 = innerElement.Value;
                     break;
                  case "City":
                     addressValidationAPIModel.City = innerElement.Value;
                     break;
                  case "State":
                     addressValidationAPIModel.State = innerElement.Value;
                     break;
                  case "Zip5":
                     addressValidationAPIModel.Zip = innerElement.Value;
                     break;
                  case "Zip4":
                     addressValidationAPIModel.Zip = String.Format("{0}-{1}",
                           addressValidationAPIModel.Zip, innerElement.Value);
                     break;
                  default:
                     break;
               }
            }
         }
         return addressValidationAPIModel;
      }
   }
}

No comments:

Post a Comment