Skip to content

Instantly share code, notes, and snippets.

@jrsconfitto
Last active December 18, 2015 12:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jrsconfitto/5782096 to your computer and use it in GitHub Desktop.
Save jrsconfitto/5782096 to your computer and use it in GitHub Desktop.
My Nancy root path hacks for debugging views quickly when self-hosting

Editing Nancy ViewEngine code directly when self-hosting

Here's how i allow myself to directly edit my view code in my Visual Studio project and then see those edits when i refresh the browser.

i use two tricks:

  1. Output the path to my working directory to a file (in the output path) from a post-build script
  2. Pick up that file and set my RootPath to its contents

Works on my machine :)

echo "$(ProjectDir)Web" > "$(OutDir)Debug-Path.txt"
// Works as of Nancy 0.17.1 i think
using System.IO;
public class MyRootPathProvider : Nancy.IRootPathProvider
{
public string GetRootPath()
{
#if DEBUG
// Use the project directory in `DEBUG` mode so i can edit my resources in VS instead of some other gymnastics
string debugHintFilePath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Debug-Root-Path.txt");
if (File.Exists(debugHintFilePath))
{
String debugRootPath = File.ReadAllText(debugHintFilePath).Replace("\"", "").Trim();
if (Directory.Exists(debugRootPath))
{
return debugRootPath;
}
}
else
{
throw new Exception("Your root path has not been set correctly. Please make sure you have permissions to write to this project's output path. You can also check the post-build event to make sure it's working correctly.");
}
#endif
return Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Web");
}
}
@Joevdwalt
Copy link

Thanks this helps me alot. Just check your build task is not 100%. It writes to Debug-Path.txt but looks for the file Debug-Root-Path.txt

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment