Blog Archives
Custom Web.Config Wrapper Class ASP .NET
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Configuration; namespace AIS.Common { public class ConfigValues { #region appSettings public static string SomeStringOne { get { return getval("SomeStringOne "); } } public static string SomeStringTwo { get { return getval("SomeStringTwo "); } } public static string Env { get { return getval("env"); } } //keep in mind case sensitivity public static string LastManualRefresh { get { return getval("date_last_manual_refresh"); } } //useful for manual site refresh/reload public static double SomeDouble { get { return Convert.ToDouble(getval("some_static_double")); } } #endregion #region connectionStrings - update web.config env variable to toggle between dev and prd public static string YourDBOneConnectionString { get { return getcstr("win_web_db"); } } //also read only implementation like above, but illustrates environment variable usage specific in web.config useful if you have many environments public static string YourDBTwoConnectionString { get { if (Env.ToLower().ToString() != "filesystem") { return getcstr("static_string" + Env.ToLower().ToString()); } return ""; } } #endregion /// <summary> /// Retrieve Connection String for specified provided key /// </summary> /// <param name="key"></param> /// <returns></returns> private static string getcstr(string key) { try { return ConfigurationManager.ConnectionStrings[key].ConnectionString; } catch (Exception ex) { Shared.HandleError(ex); //TODO: change to throw error event handle instead of direct call for reusability return "Error retrieving value"; } } /// <summary> /// Retrieve appSettings value for provided specified key /// </summary> /// <param name="key"></param> /// <returns></returns> private static string getval(string key) { try { return ConfigurationManager.AppSettings[key]; } catch (Exception ex) { Shared.HandleError(ex); //TODO: change to throw error event handle instead of direct call for reusability return "Error retrieving value"; } } } }
Custom URLRewriting with XML and Global.asax
utilizes my custom DocParser class here https://ronniediaz.com/2013/08/23/parse-xml-to-dynamic-expandoobject-c-net-4-0/
void Application_BeginRequest(object sender, EventArgs e) { //string path = Request.Url.ToString(); string path = Request.Path; if (Regex.IsMatch(path.ToLower(), ".*[.](jpg|png|gif|css|js|pdf|ico|woff|svg|eot|ttf)$")) { //var h = Regex.Replace(path.ToLower(), ".*/(.*)/(.*)$", "~/$1/$2", RegexOptions.Singleline); //Context.RewritePath(path); return; } //TODO: authorize user/request DocParser dp = new DocParser(Server.MapPath("~/rewrite.xml")); var rules = dp.GetElements("*/rule"); foreach (var r in rules) { if (string.IsNullOrEmpty(r.match_url)) { Shared.HandleError("Global.asax::null or empty match_url encountered"); } if (string.IsNullOrEmpty(r.action_url)) { Shared.HandleError("Global.asax::null or empty action_url encountered"); } if (Regex.IsMatch(path, r.match_url,RegexOptions.IgnoreCase)) { List<object> qsvars = new List<object>(); foreach (Match m in Regex.Matches(path, r.match_url, RegexOptions.IgnoreCase)) { for (int i=1;i<m.Groups.Count;i++) { qsvars.Add(m.Groups[i].Value); } } //Context.RewritePath(string.Format(r.action_url,qsvars.ToArray()) + "&rewrite=1"); Context.RewritePath(string.Format(r.action_url, qsvars.ToArray())); } } }
rewrite.xml examples
<?xml version="1.0" encoding="utf-8" ?> <rules> <rule match_url="^/home" action_url="~/home.aspx" /> <rule match_url="^/login" action_url="~/default.aspx" /> <rule match_url="^/register" action_url="~/welcome/register.aspx" /> <rule match_url="^/logout" action_url="~/logout.aspx" /> <rule match_url="^/default/(.*)" action_url="~/default.aspx?q={0}" /> <rule match_url="^/test/(.*)/(.*)" action_url="~/test.aspx?q={0}&r={1}" /> </rules>