Skip to content

Instantly share code, notes, and snippets.

@ToJans
Created March 7, 2011 15:32
Show Gist options
  • Save ToJans/858641 to your computer and use it in GitHub Desktop.
Save ToJans/858641 to your computer and use it in GitHub Desktop.
An example convention for nancy
public string ResolveViewPath(string appviewpath, string viewname,string modulename,string[] allowedextensions)
{
// find all possible extensions for this viewname
var possibleExtensions = new List<string>();
var ext = Path.GetExtension(viewname);
if (string.IsNullOrEmpty(ext))
possibleExtensions.AddRange(allowedextensions);
else
possibleExtensions.Add(ext);
// get the filename without an extension
var fn = Path.GetFileNameWithoutExtension(viewname);
// get the relevant folder
var folder =Path.GetDirectoryName(viewname);
// if the modulename ends with "Module", remove that from the modulename
if (modulename.EndsWith("Module",StringComparison.InvariantCultureIgnoreCase))
modulename = modulename.Substring(0,modulename.Length-6);
foreach(var e in possibleExtensions)
{
var newfn = fn+"."+e;
var res =
CombinePathsAndVerify(appviewpath,folder,newfn)
?? CombinePathsAndVerify(appviewpath,folder,modulename,newfn)
?? CombinePathsAndVerify(appviewpath,modulename,folder,newfn);
if (res!=null)
return res;
}
throw new Exception();
}
public static string CombinePathsAndVerify(string fn,params string[] parts)
{
string p = null;
foreach(var prt in parts)
p = p==null?prt:Path.Combine(p,prt);
if (File.Exists(p))
return p;
else
return null;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment